【问题标题】:The servlets named [Myclass] and [mypropackage.Myclass] are both mapped to the url-pattern [/myclass] which is not permitted [duplicate]名为 [Myclass] 和 [mypropackage.Myclass] 的 servlet 都映射到不允许的 url 模式 [/myclass] [重复]
【发布时间】:2015-04-26 12:00:11
【问题描述】:

启动服务器时出现以下错误

Server Tomcat v7.0 Server at localhost 启动失败。

当我查看控制台是否有错误时,我认为这是问题“名为 [Myclass] 和 [mypropackage.Myclass] 的 servlet 都映射到了不允许的 url 模式 [/myclass]”。

这里有什么问题以及如何解决它?

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>myproject</display-name>
   <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>
   <servlet>
      <servlet-name>Myclass</servlet-name>
      <servlet-class>mypropackage.Myclass</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>Myclass</servlet-name>
      <url-pattern>/myclass</url-pattern>
   </servlet-mapping>
</web-app>

Servlet 类代码:

package mypropackage;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 /**
 * Servlet implementation class Myclass
 */
 @WebServlet("/myclass")
 public class Myclass extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Myclass() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#service(HttpServletRequest request,    
  HttpServletResponse response)
     */
    protected void service(HttpServletRequest request,    
    HttpServletResponse response) throws ServletException, IOException {
        System.out.println("service method");
        String uname = "user";
        String pass = "abcd";
        String un = request.getParameter("username");
        String pw = request.getParameter("password");
        String msg = " ";
        if(un.equals(uname) && pw.equals(pass))
        {
            msg = "hello" + un + "login successful";
        }
        else
        {
            msg = "hello" + pw + "login failed";
        }

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println(msg);

    }

  }

这是控制台中的日志:

Caused by: java.lang.IllegalArgumentException: The servlets named [Myclass] and [mypropackage.Myclass] are both mapped to the url-pattern [/myclass] which is not permitted
    at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:293)
    at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2428)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2103)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2064)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2057)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1304)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5412)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more

【问题讨论】:

  • 您正在 XML 和注释级别进行映射。删除其中任何一个。

标签: eclipse servlets illegalargumentexception servlet-mapping


【解决方案1】:

你确实定义了你的 servlet 两次:

  1. 在类mypropackage.MyClass中使用@WebServlet("/myclass")注解

  2. 在定义 servlet Myclass 的地方使用 web.xml

两个 servlet 实例都映射到同一个 URL,这就是容器告诉你的。您应该只使用一种方法来定义 servlet:使用注解或使用 web.xml。我更喜欢使用web.xml,但这可能是因为我曾经在注释发明之前就在那里定义了servlts。因此,欢迎您自己做出选择。

恕我直言,使用 web.xml 更加灵活。您不必在开发时决定要映射 servlet 的 URL 是什么,并且可以多次部署同一个 servlet 并将其映射到多个 URL。

【讨论】:

    猜你喜欢
    • 2016-11-26
    • 2016-03-07
    • 1970-01-01
    • 2013-01-20
    • 2014-10-01
    • 2014-01-31
    • 1970-01-01
    • 2014-01-22
    • 2013-09-19
    相关资源
    最近更新 更多