论坛首页 Java企业应用论坛

tomcat修改web.xml文件名

浏览 3726 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-06-11  
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>
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics