本文介绍了java.lang.IllegalArgumentException:名为[X]和[Y]的servlet都映射到不允许的url-pattern [/ url]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试添加这个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>

但是,我收到以下异常:

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

解决方案

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



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



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



阅读更多:和。


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.

解决方案

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.

这篇关于java.lang.IllegalArgumentException:名为[X]和[Y]的servlet都映射到不允许的url-pattern [/ url]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 09:54