- importjavax.servlet.ServletContext;
- importorg.apache.catalina.Globals;
- importorg.apache.catalina.Lifecycle;
- importorg.apache.catalina.LifecycleEvent;
- importorg.apache.catalina.LifecycleListener;
- importorg.apache.catalina.core.StandardContext;
- /**
- *指定tomcat加载web.xml文件位置<br>
- *配置如下:修改TOMCAT_HOME/conf/servlet.xml内的context标签,在其之间加入如下语句<br>
- *<ListenerclassName="SpecifyWebXmlLifecycleListener"webXmlPath="/WEB-INF/zdb.xml"/><br>
- *<br>
- *然后拷贝本类到TOMCAT_TOMCAT/lib/目录下.
- *@authorzhangdb
- *
- */
- publicclassSpecifyWebXmlLifecycleListenerimplementsLifecycleListener{
- //web.xml文件相对位置
- privateStringwebXmlPath;
- publicvoidlifecycleEvent(LifecycleEventevent){
- Lifecyclelifecycle=event.getLifecycle();
- if("before_start".equalsIgnoreCase(event.getType())){
- StandardContextcontext=(StandardContext)lifecycle;
- ServletContextservletContext=context.getServletContext();
- StringrealWebXmlPath=context.getDocBase()+this.webXmlPath;
- servletContext.setAttribute(Globals.ALT_DD_ATTR,realWebXmlPath);
- System.out.println("substitutionweb.xmlfile:["+realWebXmlPath
- +"]!");
- }
- }
- publicStringgetWebXmlPath(){
- returnwebXmlPath;
- }
- publicvoidsetWebXmlPath(StringwebXmlPath){
- 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>