IllegalArgumentException:名为[myservlet]和[MyServlet]的servlet都映射到url-pattern [/ MyServlet],这是不允许的

时间:2015-12-24 11:02:32

标签: servlets mapping url-pattern

我在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";
}

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

&#13;
&#13;
<url-pattern>/MyServlet.java</url-pattern>
<url-pattern>/MyServlet</url-pattern>
&#13;
&#13;
&#13;