IllegalArgumentException:名为[test]和[pack.HelloWorld]的servlet都映射到url-pattern [/ another / path],这是不允许的

时间:2014-05-02 16:55:28

标签: eclipse tomcat servlets mapping

在web.xml中更改我的servlet URL后:

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>pack.HelloWorld</servlet-class>
  </servlet> 

  <servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/another/path</url-pattern>
 </servlet-mapping>

并更改servlet本身的注释。

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

我无法再从Eclipse启动servlet:

  

java.lang.IllegalArgumentException:名为[test]和[pack.HelloWorld]的servlet都映射到不允许的url-pattern [/ another / path]

它背后的原因是什么?如何在不从servlet中删除注释的情况下解决它?

堆栈跟踪:

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorld2]]
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
    at java.util.concurrent.FutureTask.get(FutureTask.java:111)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:801)
    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(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:701)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorld2]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 7 more
Caused by: java.lang.IllegalArgumentException: The servlets named [test] and [pack.HelloWorld] are both mapped to the url-pattern [/another/path] which is not permitted
    at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:292)
    at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2485)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2161)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2122)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2115)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1295)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:880)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:378)
    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:5343)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more
May 2, 2014 7:16:26 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
    at java.util.concurrent.FutureTask.get(FutureTask.java:111)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:690)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:622)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    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(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:701)
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:801)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more

1 个答案:

答案 0 :(得分:1)

尝试删除

<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/another/path</url-pattern>
</servlet-mapping>

@WebServlet("/another/path")

您不应该混合使用不同的方法将servlet映射到URL。

我还建议检查pack.HelloWorld和班级HelloWorld extends HttpServlet是否真的相同。