在 web.xml 中引用环境变量

2022-01-15 00:00:00 environment-variables java web.xml

我正在预打包一个 JSP Web 应用程序,它依赖于 web.xml 中的一些文件路径设置.这些设置在打包时是未知的,因为它们引用了客户在部署整个应用程序时将设置的路径(其中 web-app 是一个管理界面).

I'm pre-packaging a JSP web-app that relies on some file path settings found within web.xml. These settings are unknown at packaging time, because they reference a path the customer will set when deploying the entire application (of which the web-app is a management interface).

似乎在我的安装程序脚本中避免令牌和文件修改的最简单方法是询问用户安装位置,将此位置设置为环境变量(例如 JAVA_HOME),并让 web.xml 始终引用该位置变量.

It seems that the easiest way to avoid tokens and file modifications in my installer script, is to ask the user for an install location, set this location as an environment variable (e.g JAVA_HOME), and have web.xml always reference that variable.

有没有办法从 web.xml 中引用环境变量值?谷歌搜索导致从 ejb xml 文件中设置环境变量的 J2EE 方法.这不是我要找的.

Is there a way to reference an environment variable value from within web.xml? Google searches lead to the J2EE method of SETTING environment variables from ejb xml files. This is not what I'm looking for.

推荐答案

您可以在任何 tomcat xml 配置文件中使用 Ant 样式的变量替换,例如:

You can use Ant-style variable substitution in any of the tomcat xml config files, such as:

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>${foo}</url-pattern>
</servlet-mapping>

其中 foo 是 Java 系统属性(系统属性).

Where foo is a Java System Property (sysprop).

您不能直接使用 OS 环境变量 (envvars),我认为...

You can't use OS Environment Variables (envvars) directly, I think...

要使用envvars,你可以把

To use envvars, you can put

set "CATALINA_OPTS=-DsomeJavaSysProp=%SOME_OS_ENVVAR%"

bin/setenv.bat 中(或类似地在 bin/setenv.sh 中用于 *nix).您可能需要创建该文件.Tomcat 启动时会运行这个文件.

in bin/setenv.bat (or similarly in bin/setenv.sh for *nix). You may need to create that file. Tomcat will run this file when it starts.

由于 CATALINA_OPTS 是一个环境变量(与命令行选项相反),它不应该被系统上的其他用户看到(保存古老的 Unix),尽管我没有对此进行测试.

As CATALINA_OPTS is an envvar (as opposed to a command line option), it should not be visible by other users on the system (save ancient Unixes), though I haven't tested this.

http://tomcat.apache.org/tomcat-7.0-doc/config/

如果你使用 Spring,你可以创建一个 <context:property-placeholder/> bean,然后在 Spring XML 配置文件中直接使用 envvars 或 sysprops(虽然不是 web.xml).

If you are using Spring, you can create a <context:property-placeholder/> bean and then directly use envvars or sysprops in Spring XML config files (though not web.xml).

相关文章