简体   繁体   English

如何从TomCat中运行的Java应用程序中读取Apache Httpd Env变量?

[英]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 via request.getAttribute(attributeName) . 可以通过request.getAttribute(attributeName)在Tomcat端检索变量作为请求属性。 Note that the variables send via JkEnvVar will not be listed in request.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.

相关问题 如何将运行在Apache Tomcat 5中的Java应用程序迁移到Apache Tomcat 7? - How to migrate a java application running in Apache Tomcat 5 to Apache Tomcat 7? 如何读取在 apache tomcat 中运行的 web 应用程序的清单文件? - How do I read the manifest file for a webapp running in apache tomcat? 如何使用骆驼 http 组件将文件上传到运行在 apache tomcat 服务器上的应用程序? - How can I use camel http compponent to upload a file to an application running on apache tomcat server? 我怎么知道tomcat是否在Apache后面运行? - How can I know if tomcat is running behind Apache? 如何获取在 Apache Tomcat 上运行的应用程序的 maxConnections - How to get maxConnections of an application running on Apache Tomcat 如何将数据从Android应用程序发送到Java Server(Apache tomcat)? - How to send data from Android application to Java Server(Apache tomcat)? 如何在 Java 应用程序中将 Apache Tomcat 嵌入从 9.0.35 到 10.0.4 - How to update Apache Tomcat Embed from 9.0.35 to 10.0.4 in Java application Apache httpd和Tomcat如何协同工作? - How do Apache httpd and Tomcat work together? Java | Apache Tomcat 9 | 从 memory 读取密钥库 - Java | Apache Tomcat 9 | Read keystore from memory 适用于大型项目的Apache.Httpd + Php或Apache.Tomcat + Java - Apache.Httpd+Php or Apache.Tomcat+Java for large project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM