IllegalArgumentException:名为[program]和[com.test.Servlet]的servlet都映射到不允许的url-pattern [/ Servlet]

时间:2016-07-22 17:07:47

标签: eclipse tomcat servlets illegalargumentexception servlet-mapping

我刚刚开始研究Servlets。我创建了一个动态Web项目,我试图将servlet-api.jar复制到lib中。它没有成功。因此,我通过进入驱动器中的位置手动复制工作区中的jar,然后将其配置为构建路径。当我运行tomcat server 8.0时,它停止了一些错误。当我在web.xml中注释掉servlet配置时,服务器启动。配置存在一些问题。希望以下错误有所帮助。

Java代码

 package com.test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

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

     public Servlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     System.out.println("In Do Get");
    response.getWriter().append("Served at: ").append(request.getContextPath());
}

  /**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("In DO Post");
    doGet(request, response);
}

}

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org /xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Servlet</display-name>
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>    
</welcome-file-list>
<servlet>
  <servlet-name>program</servlet-name>
  <servlet-class>com.test.Servlet</servlet-class>
 </servlet>
  <servlet-mapping>
    <servlet-name>program</servlet-name>
    <url-pattern>/Servlet</url-pattern>
   </servlet-mapping>
 </web-app>

错误

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException:  org.apache.catalina.LifecycleException: Failed to start component  [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Servlet]]
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: Failed to start component   [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Servlet]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
... 6 more
Caused by: java.lang.IllegalArgumentException: The servlets named [program] and [com.test.Servlet] are both mapped to the url-pattern [/Servlet] 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:2373)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2055)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1940)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1147)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:779)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:306)
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:5150)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
... 6 more

2 个答案:

答案 0 :(得分:0)

  

名为[program]和[com.test.Servlet]的servlet都映射到不允许的url-pattern [/Servlet]

删除冲突的servlet

  <servlet-mapping>
    <servlet-name>program</servlet-name>
    <url-pattern>/Servlet</url-pattern>
   </servlet-mapping>

答案 1 :(得分:0)

通过在Java代码中删除“@WebServlet(”/ Servlet“)”解决了这个问题,因为tomcat正在检查两个具有相同名称的servlet。