在 web.xml 中引用环境变量 [英] Referencing Environment Variables in web.xml

查看:84
本文介绍了在 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>

其中 fooJava 系统属性(系统属性).

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

这篇关于在 web.xml 中引用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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