为什么 Tomcat 使用与已部署的网络应用同名的路径为 "bonus" 上下文提供服务?

Why does Tomcat serve "bonus" context with a path which has the same name with the deployed web app?

我使用 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 &quot;%r&quot; %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
   }
}

我可以看到 "abnormal" 控制台输出("ServletContext is initilizing..." 的两倍,即 contextInitialized 函数被调用了两次)。

更有趣的是,Web 应用程序可以通过上下文路径指定的完整应用程序名称 (http://localhost/ServletContextListenerTester/) and its abbreviated name(http://localhost/ContextListenerTester/) 访问。

也许具有完整应用程序名称的 "bonus" 上下文可以解释为什么 ServletContextListener.contextInitialized() 被调用两次。

我想知道 "bonus" 上下文存在的原因。

感谢您的关注!

Tomcat 正在执行此操作 双重部署 因为您已要求它这样做:

    根据您的配置,
  1. Tomcat 在 /ContextListenerTester 上部署您的 Web 应用程序,给定来自 server.xml<Context> 1. Tomcat 自动部署您的 Web 应用程序来自 webapps/ServletContextListenerTester 因为你的 <Host> 设置了 autoDeploy="true"

不想双重部署就别求了

如果您希望在 /ContextListenerTester 找到您的 Web 应用程序,请将您的 WAR 文件(或展开的 WAR 目录)命名为 ContextListenerTester(而不是 ServletContextListenerTester) 并从 server.xml.

中删除 <Context>

您根本不应再将 <Context> 放入 server.xml:有 much better ways to do it.