java.lang.IllegalArgumentException:名为[X]和[Y]的servlet都映射到不允许的url-pattern [/ url]

时间:2013-04-29 03:02:36

标签: eclipse tomcat servlets mapping illegalargumentexception

我尝试添加此servlet

package com.classmgt.servlet;

@WebServlet("/ControllerServlet")
public class ControllerServlet extends HttpServlet {}

到我的Eclipse项目,编辑web.xml如下

<servlet>
    <description>Servlet to print out Hello World!</description>
    <display-name>ControllerServlet</display-name>
    <servlet-name>ControllerServlet</servlet-name>
    <servlet-class>com.classmgt.servlet.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ControllerServlet</servlet-name>
    <url-pattern>/ControllerServlet</url-pattern>
</servlet-mapping>

但是,我得到以下例外:

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ClassManagementSystem]]
    at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    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[/ClassManagementSystem]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 7 more
Caused by: java.lang.IllegalArgumentException: The servlets named [ControllerServlet] and [com.classmgt.servlet.ControllerServlet] are both mapped to the url-pattern [/ControllerServlet] which is not permitted
    at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:335)
    at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2457)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2139)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2100)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2093)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2093)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2093)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1300)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5269)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more

我尝试将metadata-complete="true"添加到web.xml,但它不再识别servlet。

6 个答案:

答案 0 :(得分:30)

  

引起:java.lang.IllegalArgumentException:名为的servlet   [ControllerServlet]和[com.classmgt.servlet.ControllerServlet]是   两者都映射到url-pattern [/ ControllerServlet],而不是   允许

您似乎已经混合@WebServlet基于注释和基于web.xml的配置。

我怀疑您是使用“创建Servlet”向导创建了一个Servlet,该向导使用url-pattern创建web.xml条目,然后添加了@WebServlet注释,该注释会复制您在web.xml中放置的任何内容。

你应该使用其中一个,而不是两个。从web.xml中删除映射,然后仅使用@WebServlet注释。

了解详情:Servlet 3.0 Annotationsour Servlets wiki page

答案 1 :(得分:5)

只需从@WebServlet("/ControllerServlet")删除注释ControllerServlet,因为它已添加到web.xml

答案 2 :(得分:2)

java.lang.IllegalArgumentException: The servlets named...

我找到了这个原因,我在不同的包中创建了新的servlet(name ='syncro')。我的servlet位于 syncro.SynchronizeServlet 中 当我在部署描述符(web.xml)中添加有关此servlet的信息时,我发现错误: IllegalArgumentException

描述符部分不正确的示例:

<servlet>
    <description></description>
    <display-name>SynchronizeServlet</display-name>
    <servlet-name>SynchronizeServlet</servlet-name>
    <servlet-class>SynchronizeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SynchronizeServlet</servlet-name>
    <url-pattern>/SynchronizeServlet</url-pattern>
    <url-pattern>/SynServlet</url-pattern>
  </servlet-mapping>

当我为servlet添加正确的路径时 - 错误消失了。正确的desc如下:

<servlet>
    <description></description>
    <display-name>syncro.SynchronizeServlet</display-name>
    <servlet-name>syncro.SynchronizeServlet</servlet-name>
    <servlet-class>syncro.SynchronizeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>syncro.SynchronizeServlet</servlet-name>
    <url-pattern>/SynchronizeServlet</url-pattern>
    <url-pattern>/SynServlet</url-pattern>
  </servlet-mapping>

==&GT; 73!

答案 3 :(得分:2)

对我有用的是“干净”。

我的问题是在重命名Servlet类时引起的。但是,原始.class文件保留在目标目录中(使用其Servlet注释)。看起来您已将ControllerServlet移动到一个包中。

Jetty似乎并不介意这些明显的重复,但Tomcat7给了你 '都映射到url-pattern'例外。

查看这是否导致您的问题的简单方法是查看WAR以查看旧类(在您的情况下是[ControllerServlet]和[com.classmgt.servlet.ControllerServlet])是否都存在。< / p>

答案 4 :(得分:0)

至于我,我将tom-cat版本添加到我的pom文件中并且有效

<properties>
    <tomcat.version>7.0.52</tomcat.version>
</properties>
<dependencies>

答案 5 :(得分:-1)

  

名为[Register]和[com.TeamWork.controller.Register]的servlet。   都映射到网址模式[/ Register],这是不允许的

遇到此错误,您必须从web.xml中删除servlet映射,而只需添加@WebServlet(“ / Register”)批注+ url

<servlet>
     <servlet-name>Register</servlet-name>
     <servlet-class>com.TeamWork.controller</servlet-class>
  </servlet>

然后将您的servlet类添加到顶部

@WebServlet("/Register")`
public class Register extends HttpServlet { }

可以正常工作