我使用eclipse在tomcat中部署了一个Web应用程序 . 由于Web应用程序名称较长(例如,ServletContextListenerTester),我想通过缩写路径(ContextListenerTester)访问它,我的配置如下:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
<Context docBase="ServletContextListenerTester" path="/ContextListenerTester" reloadable="true" source="org.eclipse.jst.jee.server:ServletContextListenerTester"/>
</Host>
一切都运行良好,除了ServletContextListener.contextInitialized()被调用两次 . 我实现的ServletContextListener代码如下:
public class InitialListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("****************************");
System.out.println("ServletContext is initilizing...");
System.out.println("****************************");
}
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
}
我可以看到“异常”控制台输出(两次“ServletContext正在启动...”,即contextInitialized函数被调用两次) .
更有趣的是,Web应用程序可以通过Context的路径指定的完整应用程序名称(http://localhost/ServletContextListenerTester/)和缩写名称(http://localhost/ContextListenerTester/)进行访问 .
也许具有完整应用名称的“奖励”上下文可以解释为什么ServletContextListener.contextInitialized()被调用两次 .
我想知道为什么存在“奖金”背景 .
感谢您的关注!
1 回答
Tomcat正在执行此双重部署,因为您已要求它执行此操作:
server.xml
在/ContextListenerTester
上部署您的Web应用程序1. Tomcat从webapps/ServletContextListenerTester
自动部署您的Web应用程序,因为<Host>
已设置autoDeploy="true"
.如果您不想进行双重部署,请不要求它 .
如果您希望在
/ContextListenerTester
找到您的Web应用程序,请将您的WAR文件(或爆炸的WAR目录)命名为ContextListenerTester
(而不是ServletContextListenerTester
)并从server.xml
中删除<Context>
.您根本不应该将
<Context>
放入server.xml
:有much better ways to do it .