如何从TomCat中运行的Java应用程序中读取Apache Httpd Env变量? [英] How can I read Apache Httpd Env variables from a Java application running in TomCat?

查看:211
本文介绍了如何从TomCat中运行的Java应用程序中读取Apache Httpd Env变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Apache Httpd后面的TomCat容器中运行了几个Java应用程序。在Apache Httdp中,如果安装了mod_env,则可以使用 SetEnv FOO bar 设置Env变量。如何在TomCat中运行的Java应用程序中读取这些变量?
Java应用程序主要使用Stripes构建,如果有帮助的话。

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.

推荐答案

因为Tomcat是在Apache之外启动的无法访问Apache环境。这意味着您需要一些方法将环境变量从Apache传递到Tomcat。

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.

如果您使用 mod_jk连接Apache和Tomcat ,您可以使用 JkEnvVar 指令将特定变量传递给Tomcat。来自 mod_jk文档

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:


指令 JkEnvVar 允许您将环境变量从Apache服务器转发到Tomcat引擎。您可以将默认值作为第二个参数添加到指令中。如果未明确给出默认值,则仅在运行时设置变量时才发送变量。
可以通过 request.getAttribute(attributeName)在Tomcat端检索变量作为请求属性。请注意,通过 JkEnvVar 发送的变量不会列在 request.getAttributeNames()中。

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

如果您使用HTTP代理( mod_proxy )而不是 mod_jk ,您可以使用 mod_headers 将环境变量作为请求标头传递,类似于:

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

...然后在Tomcat中你必须提取 X-MYVAR 标题。

...and then in Tomcat you would have to extract the X-MYVAR header.

这篇关于如何从TomCat中运行的Java应用程序中读取Apache Httpd Env变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆