How to read Apache Httpd Env variables from a Java application running in TomCat?

I have several Java applications running in TomCat containers behind Apache Httpd. In Apache Httdp, you can set Env variables with SetEnv FOO bar

if you have mod_env installed. How can I read these variables in my Java applications running inside TomCat? Java applications are mostly built with Stripes if that helps.

+3


source to share


2 answers


Since Tomcat runs outside of Apache, it does not have access to the Apache environment. This means that you need to pass environment variables from Apache to Tomcat somehow.

If you are connecting Apache and Tomcat using mod_jk

, you can use a directive JkEnvVar

to pass certain Tomcat variables. From mod_jk documentation :

The directive JkEnvVar

allows you to redirect environment variables from the Apache server to the Tomcat engine. You can add a default value as the second parameter to the directive. If no default is explicitly specified, the variable will only be dispatched if set at runtime. Variables can be accessed on the Tomcat side as request attributes via request.getAttribute(attributeName)

. Note that passing variables through JkEnvVar

will not be specified in request.getAttributeNames()

.



If you're using HTTP proxy ( mod_proxy

) instead mod_jk

, you can pass environment variables as request headers using mod_headers

something like:

RequestHeader set X-MYVAR %{MYVAR}e

      

... and then in Tomcat you will need to extract the header X-MYVAR

.

+8


source


Also if you are using a module proxy via AJP i.e. mod_proxy_ajp

, according to the docs :



Environment variables whose names are prefixed with AJP_ are sent to the origin server as attributes of the AJP request (with the AJP_ prefix removed from the key name).

+2


source







All Articles