- import javax.servlet.ServletContext;
- import org.apache.catalina.Globals;
- import org.apache.catalina.Lifecycle;
- import org.apache.catalina.LifecycleEvent;
- import org.apache.catalina.LifecycleListener;
- import org.apache.catalina.core.StandardContext;
- /**
- * 指定tomcat加载web.xml文件位置<br>
- * 配置如下:修改TOMCAT_HOME/conf/servlet.xml内的context标签,在其之间加入如下语句<br>
- * <Listener className="SpecifyWebXmlLifecycleListener" webXmlPath="/WEB-INF/zdb.xml"/><br>
- * <br>
- * 然后拷贝本类到TOMCAT_TOMCAT/lib/目录下.
- * @author zhangdb
- *
- */
- public class SpecifyWebXmlLifecycleListener implements LifecycleListener{
- // web.xml文件相对位置
- private String webXmlPath;
- public void lifecycleEvent(LifecycleEvent event) {
- Lifecycle lifecycle = event.getLifecycle();
- if ("before_start".equalsIgnoreCase(event.getType())) {
- StandardContext context = (StandardContext) lifecycle;
- ServletContext servletContext = context.getServletContext();
- String realWebXmlPath = context.getDocBase() + this.webXmlPath;
- servletContext.setAttribute(Globals.ALT_DD_ATTR, realWebXmlPath);
- System.out.println("substitution web.xml file:[" + realWebXmlPath
- + "]!");
- }
- }
- public String getWebXmlPath() {
- return webXmlPath;
- }
- public void setWebXmlPath(String webXmlPath) {
- this.webXmlPath = webXmlPath;
- }
- }
tomcat的server.xml配置
<Context path="" docBase="E:\workspace\dyit-site\WebRoot">
<Listener className="SpecifyWebXmlLifecycleListener" webXmlPath="/WEB-INF/web_xml/jtt-wan-web.xml"/>
</Context>
<Context path="" docBase="E:\workspace\dyit-site\WebRoot">
<Listener className="SpecifyWebXmlLifecycleListener" webXmlPath="/WEB-INF/web_xml/jtt-wan-web.xml"/>
</Context>