Java servlet 404 错误 tomcat 8 但不适用于 tomcat 6

Java servlet 404 错误 tomcat 8 但不适用于 tomcat 6

我有一台在 tomcat 6 和 tomcat 8 之间来回切换的服务器。当我使用命令执行 HTTP post 时

curl -v -X POST -d @webOpenCnsRealCheatsheet.xml -H Referrer:mobileWMS,LLC -H Content-Type:text/xml http://localhost:8080/fortive2/tsmService

我在不同版本的 tomcat 之间得到了不同的结果。

首先,对于 tomcat 8,我得到:

* About to connect() to localhost port 8080 (#0)
*   Trying ::1... connected
* Connected to localhost (::1) port 8080 (#0)
> POST /fortive2/tsmService HTTP/1.1
> User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost:8080
> Accept: */*
> Referrer:mobileWMS,LLC
> Content-Type:text/xml
> Content-Length: 218
> 
< HTTP/1.1 404 
< Content-Length: 0
< Date: Mon, 20 May 2019 16:17:36 GMT
< 
* Connection #0 to host localhost left intact
* Closing connection #0

然后,对于 tomcat 6,我得到了正确的响应,没有 404 错误。发生了什么?这实际上是同一个服务器,同一个 WAR 文件。我的 servlet 是在主 tomcat web.xml ( $CATALINA_HOME/conf/web.xml ) 中定义的,而不是在 war 文件中定义的。这是故意的,因为它让我可以快速为新客户添加 web 服务 URL,而无需更改 war 文件。

另外,我会说多个 Web 应用程序引用同一个 war 文件(多个客户,因此多个网站的配置略有不同,但 UI 相同)。这对于 tomcat 6 中的 servlet 一直运行良好,所以我只是假设这对 Tomcat 8 也有效。这会造成问题吗?

另外,顺便说一句,我可以在浏览器中正常访问端口 8080,因为有时我就是这么访问网站或 tomcat 管理器的(当 SSL 出现证书问题时)。所以这不是端口的问题。

此外,作为附加信息,我有我的一个 servlet(不是我正在使用的那个,而是完全不同的 servlet):

@WebServlet(urlPatterns = "/*", name = "WmsServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = WmsUI.class, productionMode = false)
@SuppressWarnings("serial")
public class WmsServlet extends VaadinServlet {

    @Override
    protected final void servletInitialized() throws ServletException {
        super.servletInitialized();
        getService().addSessionInitListener(new WmsSessionInitListener());
    }
}

以下是一些 $CATALINA_HOME/conf/web.xml servlet 配置示例:

   <servlet>
      <servlet-name>veeder-ws</servlet-name>
      <servlet-class>com.wmsvision.servlet.WMSProcessorServlet</servlet-class>
      <init-param>
        <param-name>socketType</param-name>
        <param-value>VEEDER-WS</param-value>
      </init-param>
    </servlet>

    <servlet>
      <servlet-name>spence-ws</servlet-name>
      <servlet-class>com.wmsvision.servlet.WMSProcessorServlet</servlet-class>
      <init-param>
        <param-name>socketType</param-name>
        <param-value>SPENCE-WS</param-value>
      </init-param>
    </servlet>
   <servlet-mapping>
        <servlet-name>veeder-ws</servlet-name>
        <url-pattern>/veeder-ws/tsmService</url-pattern>
    </servlet-mapping>

   <servlet-mapping>
        <servlet-name>spence-ws</servlet-name>
        <url-pattern>/spence-ws/tsmService</url-pattern>
    </servlet-mapping>

2019 年 5 月 21 日更新:使用 Servlet Context 监听器,我在开发服务器上获得以下信息:

[2019-05-21 13:11:47 EDT] FINE com.mobiwms.website.WmsContextListener logCurrentServlets Servlet name: WMSWebsite-NEWCUST; Mappings: /NEWCUST-WebsiteXML
[2019-05-21 13:11:47 EDT] FINE com.mobiwms.website.WmsContextListener logCurrentServlets Servlet name: default; Mappings: /
[2019-05-21 13:11:47 EDT] FINE com.mobiwms.website.WmsContextListener logCurrentServlets Servlet name: WmsServlet; Mappings: /*
[2019-05-21 13:11:47 EDT] FINE com.mobiwms.website.WmsContextListener logCurrentServlets Servlet name: FortivePFC; Mappings: /Fortive-PFC
[2019-05-21 13:11:47 EDT] FINE com.mobiwms.website.WmsContextListener logCurrentServlets Servlet name: Fortive3; Mappings: /fortive3/tsmService
[2019-05-21 13:11:47 EDT] FINE com.mobiwms.website.WmsContextListener logCurrentServlets Servlet name: jsp; Mappings: *.jspx, *.jsp
[2019-05-21 13:11:47 EDT] FINE com.mobiwms.website.WmsContextListener logCurrentServlets Servlet name: Fortive2; Mappings: /fortive2/tsmService
[2019-05-21 13:11:47 EDT] FINE com.mobiwms.website.WmsContextListener logCurrentServlets Servlet name: WMSWebsite-NEWCUST2; Mappings: /NEWCUST2-WebsiteXML

以及我的开发机器的全局 web.xml 的 servlet 部分(所以是 $CATALINA_HOME/conf/web.xml ),其中打印了上述日志:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">

    <!-- snipped unrelated config -->

    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <!-- snipped unrelated config -->

    <servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>xpoweredBy</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>


    <!-- snipped unrelated config -->

  <!-- ================ Built In Servlet Mappings ========================= -->


  <!-- The servlet mappings for the built in servlets defined above.  Note  -->
  <!-- that, by default, the CGI and SSI servlets are *not* mapped.  You    -->
  <!-- must uncomment these mappings (or add them to your application's own -->
  <!-- web.xml deployment descriptor) to enable these services              -->

    <!-- The mapping for the default servlet -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- The mappings for the JSP servlet -->
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>

    <!-- snipped unrelated config -->

  <!-- ==================== Custom WMS servlet setup ===================== -->
    <servlet>
      <servlet-name>FortivePFC</servlet-name>
      <servlet-class>com.wmsvision.servlet.WMSProcessorServlet</servlet-class>
      <init-param>
        <param-name>socketType</param-name>
        <param-value>Fortive_PFC</param-value>
      </init-param>
    </servlet>

    <servlet>
      <servlet-name>Fortive2</servlet-name>
      <servlet-class>com.wmsvision.servlet.WMSProcessorServlet</servlet-class>
      <init-param>
        <param-name>socketType</param-name>
        <param-value>Fortive2</param-value>
      </init-param>
    </servlet>

    <servlet>
      <servlet-name>Fortive3</servlet-name>
      <servlet-class>com.wmsvision.servlet.WMSProcessorServlet</servlet-class>
      <init-param>
        <param-name>socketType</param-name>
        <param-value>Fortive3</param-value>
      </init-param>
    </servlet>

    <servlet>
      <servlet-name>WMSWebsite-NEWCUST</servlet-name>
      <servlet-class>com.wmsvision.servlet.WMSWebsiteServlet</servlet-class>
      <init-param>
        <param-name>socketType</param-name>
        <param-value>WMSWebsite-NEWCUST</param-value>
      </init-param>
    </servlet>

    <servlet>
      <servlet-name>WMSWebsite-NEWCUST2</servlet-name>
      <servlet-class>com.wmsvision.servlet.WMSWebsiteServlet</servlet-class>
      <init-param>
        <param-name>socketType</param-name>
        <param-value>WMSWebsite-NEWCUST2</param-value>
      </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>FortivePFC</servlet-name>
        <url-pattern>/Fortive-PFC</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Fortive2</servlet-name>
        <!-- <url-pattern>/turnsmith/tsmService</url-pattern>-->
        <url-pattern>/fortive2/tsmService</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Fortive3</servlet-name>
        <!-- <url-pattern>/NEWCUST/tsmService</url-pattern>-->
        <url-pattern>/fortive3/tsmService</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>WMSWebsite-NEWCUST</servlet-name>
        <url-pattern>/NEWCUST-WebsiteXML</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>WMSWebsite-NEWCUST2</servlet-name>
        <url-pattern>/NEWCUST2-WebsiteXML</url-pattern>
    </servlet-mapping>

</web-app>

答案1

好的,找到答案了,而且很简单。答案很简单,为了避免人们犯我的错误,不要只检查 web.xml 中的映射,还要确保上下文存在,或者确保您的 URL 包含有效上下文。因此,如果您没有在 URL 中包含上下文,请确保 ROOT.xml 配置存在。

因此,更具体地说,就我而言,我删除了 $CATALINA_HOME/conf/Catalina/localhost/ROOT.xml,但发布到了根上下文。因此,通过上面的测试,

curl -v -X POST -d @webOpenCnsRealCheatsheet.xml -H Referrer:mobileWMS,LLC -H Content-Type:text/xml http://localhost:8080/fortive2/tsmService

“fortive2” 不是上下文,它是我对 servlet 的引用的一部分。您可以在我的 web.xml 片段中看到这一点。由于 tomcat8 配置没有 ROOT.xml,因此它失败并出现 404,因为无法找到。但 tomcat6 可以工作,因为我从未从该“localhost”目录中删除 ROOT.xml。

相关内容