【问题标题】:IllegalArgumentException: The servlets named [program] and [com.test.Servlet] are both mapped to the url-pattern [/Servlet] which is not permitted [duplicate]IllegalArgumentException:名为 [program] 和 [com.test.Servlet] 的 servlet 都映射到不允许的 url 模式 [/Servlet] [重复]
【发布时间】:2016-11-26 16:21:11
【问题描述】:

我刚刚开始研究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

【问题讨论】:

  • 没有。我已经发布了我的 web.xml。我也在更新 servlet 代码。请检查一下
  • 第 1 步:阅读错误信息。第 2 步:考虑错误消息。第 3 步:请注意,虽然您只列出了 /Servlet 的一个 servlet 映射,但 Tomcat 声明有两个映射。为您的 servlet 尝试不同的映射。
  • 尝试将&lt;url-pattern&gt;/Servlet&lt;/url-pattern&gt;更改为&lt;url-pattern&gt;/Servlet/*&lt;/url-pattern&gt;
  • 好的..我明白了..我也在Java代码中添加了它。感谢您的信息

标签: eclipse tomcat servlets illegalargumentexception servlet-mapping


【解决方案1】:

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

删除有冲突的 servlet

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 您好,您可以使用旁边的复选标记将答案标记为已接受,让其他人知道问题的解决方案是什么。
    猜你喜欢
    • 2013-09-19
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2013-07-27
    • 2014-10-06
    • 2016-03-07
    • 1970-01-01
    相关资源
    最近更新 更多