IllegalArgumentException:名为 [myservlet] 和 [MyServlet] 的 servlet 都映射到不允许的 url 模式 [/MyServlet]

IllegalArgumentException: The servlets named [myservlet] and [MyServlet] are both mapped to the url-pattern [/MyServlet] which is not permitted

我在 eclipse Mars(4.5.1) 中编写了非常简单的动态 Web 应用程序,但我无法从 eclipse 启动 tomcat 服务器。以下是错误的根本原因:

Caused by: java.lang.IllegalArgumentException: The servlets named [myservlet] and [MyServlet] are both mapped to the url-pattern [/MyServlet] which is not permitted
      at org.apache.tomcat.util.descriptor.web.WebXml.addServletMapping(WebXml.java:308)
      at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2342)
      at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2024)
      at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1918)
      at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1139)
      at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:771)
      at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:305)
      at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
      at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 6 more

我遵循了这个步骤——在 属性中添加库 -> Java 构建路径 -> 添加库 -> 服务器运行时 -> Apache。之后,我添加了 Windows -> 首选项 -> Serevr -> 运行时环境 -> Apache。 Tomcat v8.0 Server 完美启动于**C:\Program Files\Apache Software Foundation\Tomcat 8.0\bin **。

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>2J2EEProcessingFromData</display-name>

  <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>Testing</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>


  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

MyServlrt.java

public class MyServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    response.setContentType("text/html;charset=ISO-8859-1");
    PrintWriter pw = response.getWriter();
    try{
        String username = request.getParameter("username");
        String password = request.getParameter("pass");

        pw.write("Hello "+username);
        pw.write("Your password is:"+password);
    } catch(Exception e){
        e.printStackTrace();
    } finally {
        pw.close();
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    PrintWriter pw = response.getWriter();
    pw.write("doGet called");
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    PrintWriter pw = response.getWriter();
    pw.write("doPost called");
    processRequest(request, response);
}

@Override
public String getServletInfo(){
    return "Shord description";
}

}

单击 Servers Tab 双击 Tomcat,然后将 Ports 部分中的 HTTP port 更改为任何其他。 或打开 Server.xml 文件并更改连接器端口。重新启动服务器,然后检查

这很有趣,因为我正在回答我自己的问题,我将 url-pattern 替换为 (.java) 扩展名,如下所示。

<url-pattern>/MyServlet.java</url-pattern>
<url-pattern>/MyServlet</url-pattern>