为什么更改WEB-INF/web.xml不会导致Tomcat重新加载上下文?

3
我使用Eclipse WTP Tomcat创建了一个名为“test”的动态Web项目,Eclipse版本为Java EE Kepler Service Release 2,Tomcat版本为7.0.53,Servlet 3.0,Win 8.1 x86,jdk 1.6.0_45。但是,在尝试多次后,我发现只有满足以下所有条件时才会出现问题:
1. Webapp(Web上下文)不在<CATALINA_HOME>/webapps中(这是appBase的默认值)且: 2. Webapp通过在<CATALINA_HOME>\conf\server.xml下的<Host>元素下编写<Context>元素进行配置。
下面是<CATALINA_HOME>\conf\server.xml的部分内容:
   <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

    <!-- SingleSignOn valve, share authentication between web applications
         Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->

    <!-- Access log processes all example.
         Documentation at: /docs/config/valve.html
         Note: The pattern used is equivalent to using pattern="common" -->
    <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="D:/tmpdev/apache-tomcat-7.0.53/wtpwebapps/test" path="/test" reloadable="true"/>
  </Host>

以下是“test”目录的目录结构:

test
├─META-INF
│      MANIFEST.MF
│
└─WEB-INF
    │  web.xml
    │
    ├─classes
    │  └─test
    │          HelloServlet.class
    │          MyFilter1.class
    │          MyFilter2.class
    │          MyHttpSessionListener.class
    │          MyServletContextListener.class
    │
    └─lib

虽然 WEB-INF/web.xml 在默认情况下是<CATALINA_HOME>\conf\context.xml中的<WatchedResource>,但我尝试添加了以下格式的<WatchedResource>元素,仍然无法正常工作:

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF\web.xml</WatchedResource>
<WatchedResource>D:/tmpdev/apache-tomcat-7.0.53/wtpwebapps/test/WEB-INF/web.xml</WatchedResource>
<WatchedResource>D:\tmpdev\apache-tomcat-7.0.53\wtpwebapps\test\WEB-INF\web.xml</WatchedResource>

以下是其他可用的配置方式:

  1. 使用一个单独的文件 <CATALINA_HOME>\conf\Catalina\localhost\test.xml 来配置上下文,或者:
  2. 将 "test" 移动到 <CATALINA_HOME>\webapps 目录下,无需进行其他配置,或者:
  3. 在 Eclipse WTP 中的服务器选项中勾选 "Serve modules without publishing"。

你把那些<WatchedResource>元素添加在哪里了? - Mark Thomas
@MarkThomas,我在conf/server.xml的<Context>元素下逐个尝试了那些<WatchedResource>元素,但是没有一个起作用! - user3658924
在每次更改 server.xml 文件后,您是否重新启动了 Tomcat? - Mark Thomas
@MarkThomas,是的,我已经重新启动了。我知道主要的conf/server.xml文件在不重启Tomcat的情况下无法重新加载。我会检查一下这个问题是否在Linux下出现。 - user3658924
@MarkThomas,我在Ubuntu下测试了相同版本的Tomcat,它的行为与Windows下的相同。我还尝试了Tomcat 7.0.8,但也没有起作用。这似乎是一个bug? - user3658924
3个回答

5
< p > <WatchedResource> 只适用于自动部署功能,当上下文从 server.xml 部署时不会使用该功能。


0

您需要在context.xml中设置 reloadable="true" privileged="true" 重新启动Tomcat服务器即可。


-2

只有在我更新了server.xml文件后,重新加载才起作用!!!

通过设置autoDeploy=true

<br>
The output from the logs then showed a "Reloading context" 

08-May-2018 20:58:56.654 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.reload <b>Reloading context </b>[/prototype_maven]
08-May-2018 20:58:56.654 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/prototype_maven] has started
08-May-2018 20:58:58.619 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
08-May-2018 20:58:58.629 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/prototype_maven] is completed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接