gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:34 26 4
gpt4 key购买 nike

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

最佳答案

因为 Tomcat 是在 Apache 之外启动的,所以它无法访问 Apache 环境。这意味着您需要某种方式将环境变量从 Apache 传递到 Tomcat。

如果您使用 mod_jk 连接 Apache 和 Tomcat,您可以使用 JkEnvVar 指令将特定变量传递给 Tomcat。来自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().

如果您使用 HTTP 代理 (mod_proxy) 而不是 mod_jk,您可以使用 mod_headers 将环境变量作为请求 header 传递,一些喜欢:

RequestHeader set X-MYVAR %{MYVAR}e

...然后在 Tomcat 中,您必须提取 X-MYVAR header 。

关于java - 如何从在 TomCat 中运行的 Java 应用程序读取 Apache Httpd Env 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14424142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com