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

放肆的年华 提交于 2019-12-05 21:39:06

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 via request.getAttribute(attributeName). Note that the variables send via JkEnvVar will not be listed in request.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.

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!