Reputation: 319
I have a couple of 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 those variables in my Java applications running inside TomCat?
The Java applications are mostly build with Stripes, if that helps.
Upvotes: 3
Views: 4718
Reputation: 1419
Also if you are using module proxy through AJP, i.e. mod_proxy_ajp
, according to the docs:
Environment variables whose names have the prefix AJP_ are forwarded to the origin server as AJP request attributes (with the AJP_ prefix removed from the name of the key).
Upvotes: 3
Reputation: 311606
Because Tomcat is started outside of Apache it does not have access to the Apache environment. This means you need some way of passing environment variables from Apache to Tomcat.
If you are connecting Apache and Tomcat using mod_jk
, you can use the JkEnvVar
directive to pass specific variables to Tomcat. From the mod_jk documentation:
The directive
JkEnvVar
allows you to forward environment variables from Apache server to Tomcat engine. You can add a default value as a second parameter to the directive. If the default value is not given explicitly, the variable will only be send, if it is set during runtime. The variables can be retrieved on the Tomcat side as request attributes viarequest.getAttribute(attributeName)
. Note that the variables send viaJkEnvVar
will not be listed inrequest.getAttributeNames()
.
If you are using the HTTP proxy (mod_proxy
) instead of 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 would have to extract the X-MYVAR
header.
Upvotes: 8