Java 在 web.xml 中引用环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1404384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Referencing Environment Variables in web.xml
提问by Udi Bar-On
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).
我正在预先打包一个依赖于 web.xml 中的一些文件路径设置的 JSP web 应用程序。这些设置在打包时是未知的,因为它们引用了客户在部署整个应用程序(其中 web 应用程序是管理界面)时将设置的路径。
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.
似乎在我的安装程序脚本中避免令牌和文件修改的最简单方法是向用户询问安装位置,将此位置设置为环境变量(例如 JAVA_HOME),并让 web.xml 始终引用该变量。
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.
有没有办法从 web.xml 中引用环境变量值?谷歌搜索导致从 ejb xml 文件设置环境变量的 J2EE 方法。这不是我要找的。
回答by ax.
i think you don't want to use environment variables (which i think are not accessible from web.xml), but environment entries[1, 2]. like so:
我认为您不想使用环境变量(我认为无法从 web.xml 访问),而是使用环境条目[ 1, 2]。像这样:
<env-entry>
<env-entry-name>Bla/SomeFilePath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>/opt/bla</env-entry-value>
</env-entry>
you can use SomeFilePath like:
你可以使用 SomeFilePath 像:
InitialContext ic = new InitialContext();
String s = (String) ic.lookup("java:comp/env/ejb/Bla/SomeFilePath");
回答by Donal Fellows
Basically, you don't do it that way. The web.xml
should contain default values for things, yes, but you should override them when actually doing the deployment. If you're deploying to Tomcat, you do this by including appropriate entries in the context.xml
that you use for the deployment. For example:
基本上,你不会那样做。本web.xml
应包含默认值的东西,是的,但你应该重写他们实际上做部署时。如果要部署到 Tomcat,则可以通过在context.xml
用于部署的 中包含适当的条目来完成此操作。例如:
<Context path="/app">
<!-- For things described by webapp parameters -->
<Parameter name="foobar" value="grill" />
<!-- For things described by environment entries -->
<Environment name="Bla/SomeFilePath" type="java.lang.String"
value="/opt/bla" />
</Context>
Other containers will have their own mechanisms for doing this so. You'll have to look up their documentation (or make your request for help more focussed).
其他容器将有自己的机制来这样做。您必须查找他们的文档(或使您的帮助请求更加集中)。
回答by Neil McGuigan
You can use Ant-style variable substitution in any of the tomcat xml config files, such as:
您可以在任何 tomcat xml 配置文件中使用 Ant 样式的变量替换,例如:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>${foo}</url-pattern>
</servlet-mapping>
Where foo
is a Java System Property(sysprop).
哪里foo
是Java系统属性(sysprop)。
You can't use OS Environment Variables(envvars) directly, I think...
你不能直接使用操作系统环境变量(envvars),我想......
To use envvars, you can put
要使用 envvars,你可以把
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.
在bin/setenv.bat
(或类似地bin/setenv.sh
用于 *nix)。您可能需要创建该文件。Tomcat 会在启动时运行这个文件。
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.
作为CATALINA_OPTS
envvar(与命令行选项相反),系统上的其他用户不应该看到它(除了古老的 Unix),尽管我还没有对此进行测试。
http://tomcat.apache.org/tomcat-7.0-doc/config/
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
).
如果您使用的是 Spring,您可以创建一个<context:property-placeholder/>
bean,然后直接在 Spring XML 配置文件中使用 envvars 或 sysprops(尽管不是web.xml
)。
回答by Toby Eggitt
I'm not totally clear on your limitations, but just maybe you can do this (I'm assuming that it's an init-param you're trying to configure):
我不完全清楚您的限制,但也许您可以这样做(我假设它是您尝试配置的 init-param):
1) Leave the variable unspecified in web.xml
2) Create a ServletContextListener and add that to your application
3) Listen for the initialization of your servlet
4) Set the init-param for the servlet at that point
1) 在 web.xml 中保留未指定的变量
2) 创建一个 ServletContextListener 并将其添加到您的应用程序
3) 监听 servlet 的初始化
4) 在该点设置servlet的 init-param
I tried this with a similar problem, but it failed for me because it turned out that the 3rd party servlet (which I also didn't want to mess with) wasn't actually behaving as a servlet at all, so the context never got initialized. But maybe it has a chance here...
我用类似的问题尝试过这个,但它对我来说失败了,因为结果证明 3rd 方 servlet(我也不想弄乱)实际上根本没有像 servlet 那样运行,所以上下文从未得到初始化。但也许它在这里有机会......
回答by Sayeed S. Alam
You have to put the env-entry in order :
您必须按顺序排列 env-entry:
<env-entry>
<env-entry-name>maxAmount</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>aString</env-entry-value>
</env-entry>
Else you will have validation error on web.xml
否则您将在 web.xml 上出现验证错误
ref: https://community.oracle.com/thread/840452?tstart=0
参考:https: //community.oracle.com/thread/840452?tstart =0
回答by E pluribus Unum
Environment variables can be accessed in xml files like this:
可以在 xml 文件中访问环境变量,如下所示:
${env.ENVIRONMENT_VARIABLE_NAME}
Obviously there may be issues with user account settings and access issues, but i have tried with system variables and it works!
显然,用户帐户设置和访问问题可能存在问题,但我已尝试使用系统变量,并且可以正常工作!