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

查看:575
本文介绍了引用web.xml中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



有没有办法从web.xml中引用环境变量值? Google搜索引发了从ejb xml文件设置环境变量的J2EE方法。这不是我正在寻找的。

解决方案

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

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

其中 foo 是一个 Java系统属性(sysprop)。



我想,您不能直接使用操作系统环境变量(envvars)。



要使用envvars,可以将

  set在$ code> bin / setenv.bat中,CATALINA_OPTS = -DsomeJavaSysProp =%SOME_OS_ENVVAR%

(或类似于 bin / setenv.sh for * nix)。您可能需要创建该文件。 Tomcat将在启动时运行此文件。



由于 CATALINA_OPTS 是一个envvar(而不是命令行选项),它不应该在系统上的其他用户可见(保存古代Unixes),虽然我还没有测试过。



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



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


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

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.

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.

解决方案

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>

Where foo is a Java System Property (sysprop).

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

To use envvars, you can put

set "CATALINA_OPTS=-DsomeJavaSysProp=%SOME_OS_ENVVAR%"

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.

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/

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天全站免登陆