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.
source to share
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 viarequest.getAttribute(attributeName)
. Note that passing variables throughJkEnvVar
will not be specified inrequest.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
.
source to share