Eclipse Tomcat - 上下文环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11260709/
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
Eclipse Tomcat - Context environment variable
提问by saravana_pc
I use eclipse + tomcat + maven.
我使用 eclipse + tomcat + maven。
When I deploy my project to Tomcat, the following entry gets added to the server.xml of the tomcat configuration:
当我将项目部署到 Tomcat 时,以下条目将添加到 tomcat 配置的 server.xml 中:
<Context docBase="C:\ws_eclipse\ws\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\MyApp" path="/myApp" reloadable="true" source="org.eclipse.jst.jee.server:MyApp"/>
What I actually want is to add an environment variable so that the context looks like this:
我真正想要的是添加一个环境变量,以便上下文如下所示:
<Context docBase="C:\ws_eclipse\ws\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\MyApp" path="/myApp" reloadable="true" source="org.eclipse.jst.jee.server:MyApp">
<Environment name="config.file" type="java.lang.String" value="C:/test/config.xml" />
</Context>
I tried adding it as an environment variable in eclipse run configuration, but when I deployed the application on tomcat, the context does not include the environment variable.
我尝试在 eclipse 运行配置中将其添加为环境变量,但是当我在 tomcat 上部署应用程序时,上下文不包含环境变量。
回答by MartinK
You could add a context.xmlfile in the META-INFdirectory of your webapp. The content of this file may look like:
您可以在web 应用程序的META-INF目录中添加一个context.xml文件。该文件的内容可能如下所示:
<Context>
<Parameter name="config.file" value="C:/test/config.xml"/>
</Context>
This has the same effect as defining a <context-param/>
in your web.xml. Use ServletContext.getInitParameter("config.file")
to retrieve it's value as a String.
这与<context-param/>
在 web.xml 中定义 a 的效果相同。用于ServletContext.getInitParameter("config.file")
以字符串形式检索它的值。
You may use Environment
as well, but it's probably overkill (You just need a String anyway, I think).
你也可以使用Environment
,但它可能有点矫枉过正(我认为你只需要一个字符串)。
See http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
见http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
回答by Tejas C
Setting it in application launch configuration is the best way, you might want to verify what you did at here - Creating a Java application launch configuration
在应用程序启动配置中设置它是最好的方法,您可能想验证您在此处所做的操作 -创建 Java 应用程序启动配置
HTH
HTH