java.lang.IllegalArgumentException:名为[X]和[Y]的servlet都映射到不允许的url-pattern [/ url](java.lang.IllegalArgumentException: The servlets named [X] and [Y] are both mapped to the url-pattern [/url] which is not permitted)

2835 IT屋

I tried to add this servlet

package com.classmgt.servlet;

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

to my Eclipse project, by editing the web.xml as below

<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>

However, I got the following exception:

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

I have tried adding metadata-complete="true" to web.xml, but it does not recognize the servlet anymore.

解决方案

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

It seems that you have mixed @WebServlet annotation based and web.xml based configuration.

I doubt that you created a Servlet using the "Create Servlet" wizard which creates web.xml entry with url-pattern and then , added a @WebServlet annotation which duplicates anything you may put in the web.xml.

You should use the one or the other, not both. Remove the mapping from web.xml and go ahead with using only the @WebServlet annotation.

Read more: Servlet 3.0 Annotations and our Servlets wiki page.

我尝试添加这个servlet



  package com.classmgt.servlet; 

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


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



 < servlet的> 
< description> Servlet打印出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>


但是,我收到以下异常:



< pre> SEVERE:启动期间子容器失败
java.util.concurrent.ExecutionException:org.apache.catalina.LifecycleException:无法启动组件[StandardEngine [Catalina] .StandardHost [localhost] .StandardContext [/ ClassManagementSystem]]
在java.util.concurrent.FutureTask $ Sync.innerGet(未知源)
在java.util.concurrent.FutureTask.get(未知来源)
在org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
在org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
在org .apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1559)
在org.apache .catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1549)
在java.util.concurrent.FutureTask $ Sync.innerRun(未知酸ce)
在java.util.concurrent.FutureTask.run(未知源)
在java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)
在java.lang.Thread.run(未知来源)
导致:org.apache.catalina.LifecycleException:无法启动组件[StandardEngine [Catalina] .StandardHost [localhost] .StandardContext [/ ClassManagementSystem]]
在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
... 7更多
引起的:java .lang.IllegalArgumentException:名为[ControllerServlet]和[com.classmgt.servlet.ControllerServlet]的servlet都映射到org.apache.catalina.deploy中不允许的
的url-pattern [/ ControllerServlet]。 Web.Xml.addServletMapping nfig.processAnnotationsStream(ContextConfig.java:2139)
在org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2100)
在org.apache.catalina.startup.ContextConfig.processAnnotationsFile( ContextConfig.java:2093)
在org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2093)
在org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java: 2093)
在org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1300)
在org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
在org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
在org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
在org .apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
在org.apache.catalina.core.StandardContext.startInternal(St andardContext.java:5269)
在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
...另外
/ pre>

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


解决方案

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




似乎你已经混合了 @WebServlet 注释,



我怀疑您使用"创建Servlet"向导创建了一个Servlet,它创建web.xml条目与url-pattern,然后添加了一个 @WebServlet 注释,它们可以复制到web.xml中的任何内容。



你应该使用一个或另一个,而不是两者。从 web.xml 中删除​​映射,然后继续使用 @WebServlet 注释。



阅读更多: Servlet 3.0注释我们的Servlets wiki页面


本文地址:IT屋 » java.lang.IllegalArgumentException:名为[X]和[Y]的servlet都映射到不允许的url-pattern [/ url]