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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16270619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 21:41:03  来源:igfitidea点击:

java.lang.IllegalArgumentException: The servlets named [X] and [Y] are both mapped to the url-pattern [/url] which is not permitted

eclipsetomcatservletsmappingillegalargumentexception

提问by John

I tried to add this servlet

我试图添加这个 servlet

package com.classmgt.servlet;

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

to my Eclipse project, by editing the web.xmlas below

到我的 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>

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.

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

回答by Hardik Mishra

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

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

It seems that you have mixed @WebServletannotation based and web.xmlbased configuration.

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

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

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

You should use the one or the other, not both. Remove the mapping from web.xmland go ahead with using only the @WebServletannotation.

您应该使用其中一个,而不是两者都使用。删除映射web.xml并继续使用@WebServlet注释。

Read more: Servlet 3.0 Annotationsand our Servlets wiki page.

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

回答by José Alfredo Guerra Gómez

Just remove the annotation @WebServlet("/ControllerServlet"), from the ControllerServlet, because it already added in the web.xml.

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

回答by KeithJ

What worked for me is doing a 'clean'.

对我有用的是做一个“清洁”。

My issue was caused when the Servlet class was renamed. However, the original .class files remained in the target directory (with their Servlet annotation). It looks like you moved your ControllerServlet into a package.

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

Jetty didn't seem to mind these apparent duplicates, but Tomcat7 gave your 'both mapped to the url-pattern' exception.

Jetty 似乎并不介意这些明显的重复,但是 Tomcat7 给了你的“两者都映射到 url-pattern”异常。

The easy way to see if this is causing your issue is to look in the WAR to see if both the old classes (in your case [ControllerServlet] and [com.classmgt.servlet.ControllerServlet]) are both there.

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

回答by Cristopher Plasma

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

I fetched this cause where I create new servlet in different package (name='syncro'). My servlet located in syncro.SynchronizeServletAnd when I add information about this servlet in deployment descriptor (web.xml) I catch error: IllegalArgumentException

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

Example of incorrect descriptor part:

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

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

When I add correct path for servlet - error disappeared. Correct desc below:

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

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

==> 73!

==> 73!

回答by user1311061

As for me I added the tom-cat version to my pom file and it worked

至于我,我将 tom-cat 版本添加到我的 pom 文件中,并且它起作用了

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

回答by Mani Manu

The servlets named [Register] and [com.TeamWork.controller.Register] are both mapped to the url-pattern [/Register] which is not permitted

名为 [Register] 和 [com.TeamWork.controller.Register] 的 servlet 都映射到不允许的 url-pattern [/Register]

getting this error you have to remove your servlet mapping from web.xml and just add @WebServlet("/Register") annotation + url

收到此错误,您必须从 web.xml 中删除您的 servlet 映射,然后添加 @WebServlet("/Register") 注释 + url

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

then your servlet class on the top add this one

然后你在顶部的 servlet 类添加这个

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

it will work thanks

它会工作谢谢