[英]How can I read Apache Httpd Env variables from a Java application running in TomCat?
I have a couple of Java applications running in TomCat containers behind Apache Httpd. 我在Apache Httpd后面的TomCat容器中运行了几个Java应用程序。 In Apache Httdp you can set Env variables with SetEnv FOO bar
, if you have mod_env installed. 在Apache Httdp中,如果安装了mod_env,则可以使用SetEnv SetEnv FOO bar
设置Env变量。 How can I read those variables in my Java applications running inside TomCat? 如何在TomCat中运行的Java应用程序中读取这些变量? The Java applications are mostly build with Stripes, if that helps. Java应用程序主要使用Stripes构建,如果有帮助的话。
Because Tomcat is started outside of Apache it does not have access to the Apache environment. 因为Tomcat是在Apache之外启动的,所以它无法访问Apache环境。 This means you need some way of passing environment variables from Apache to Tomcat. 这意味着您需要一些方法将环境变量从Apache传递到Tomcat。
If you are connecting Apache and Tomcat using mod_jk
, you can use the JkEnvVar
directive to pass specific variables to Tomcat. 如果使用mod_jk
连接Apache和Tomcat,则可以使用JkEnvVar
指令将特定变量传递给Tomcat。 From the mod_jk documentation : 从mod_jk文档 :
The directive
JkEnvVar
allows you to forward environment variables from Apache server to Tomcat engine. 指令JkEnvVar
允许您将环境变量从Apache服务器转发到Tomcat引擎。 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)
. 可以通过request.getAttribute(attributeName)
在Tomcat端检索变量作为请求属性。 Note that the variables send viaJkEnvVar
will not be listed inrequest.getAttributeNames()
. 请注意,通过JkEnvVar
发送的变量不会列在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: 如果您使用的是HTTP代理( mod_proxy
)而不是mod_jk
,则可以使用mod_headers
将环境变量作为请求标头mod_headers
,例如:
RequestHeader set X-MYVAR %{MYVAR}e
...and then in Tomcat you would have to extract the X-MYVAR
header. ...然后在Tomcat中你必须提取X-MYVAR
标题。
Also if you are using module proxy through AJP, ie mod_proxy_ajp
, according to the docs : 此外,如果您通过AJP使用模块代理,即mod_proxy_ajp
,根据文档 :
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). 名称前缀为AJP_的环境变量将作为AJP请求属性(从密钥名称中删除AJP_前缀)转发到源服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.