简体   繁体   English

如何在Tomcat的web.xml中使用变量替换作为上下文路径

[英]How can I use variable substitution in Tomcat's web.xml for the context path

Using Tomcat as my Servlet Container, how can I deploy two identical wars to different contexts and simply use the context path as the variable which will determine which properties file to load? 使用Tomcat作为我的Servlet容器,我如何才能将两个完全相同的war部署到不同的上下文中,而仅使用上下文路径作为变量来确定要加载的属性文件? I'm looking to do something like this in web.xml: 我想在web.xml中执行以下操作:

    <context-param>
        <param-name>initialization.file</param-name>
        <param-value>
            WEB-INF/config/context${contextPath}.properties
        </param-value>
    </context-param>

and then load the initialization file based on this context property. 然后基于此上下文属性加载初始化文件。 Is this possible? 这可能吗? If so, how? 如果是这样,怎么办?

I'm using Tomcat 6, Java 6, and Servlet API 2.5. 我正在使用Tomcat 6,Java 6和Servlet API 2.5。

That depends on what you try to configure. 这取决于您尝试配置的内容。 The web.xml is static, no variables can be assigned. web.xml是静态的,不能分配任何变量。 But you can pass the variable name where it can be assigned. 但是您可以在可以分配变量的地方传递变量名。 Implement ServletContextListner and use the ServletContextEvent: 实现ServletContextListner并使用ServletContextEvent:

ServletContext servletContext = servletContextEvent.getServletContext();
String contextPath = servletContext.getContextPath();

Than you can replace the variable name with the path and load the properties file. 比起您可以用路径替换变量名并加载属性文件。 Of course, you need to add the listener to the web.xml, as the very first listener. 当然,您需要将侦听器添加到web.xml中,作为第一个侦听器。

It will be loaded at application startup, but keep in mind that you cannot make sure in which order. 它将在应用程序启动时加载,但是请记住,您无法确定顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM