20

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dan
  • 810
  • 2
  • 11
  • 29
  • Looks like there is two servlets and the same url-pattern but according to your web.xml there is only one, maybe wront web.xml is running? Try recompiling and restart? – Alexandre Lavoie Apr 29 '13 at 03:33
  • 5
    Did you mix annotation-based and web.xml-based configuration? – Jim Garrison Apr 29 '13 at 04:11
  • inside my ControllerServlet I put this: `@WebServlet("/ControllerServlet")` and at my form this is what I put: `
    `
    – Dan Apr 29 '13 at 04:24

7 Answers7

37

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.

Community
  • 1
  • 1
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
11

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

profesor79
  • 9,213
  • 3
  • 31
  • 52
3
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.SynchronizeServlet And when I add information about this servlet in deployment descriptor (web.xml) I catch error: 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>
    <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!

Mikro Koder
  • 1,056
  • 10
  • 13
3

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.

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

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.

KeithJ
  • 31
  • 1
0

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

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

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

getting this error you have to remove your servlet mapping from web.xml and just add @WebServlet("/Register") annotation + 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

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

it will work thanks

Mani Manu
  • 9
  • 2
0

For me, it was an unexpected error in Intellij Idea.

I deleted a servlet 2 days back which was having the same URL pattern. However, I am getting the error that my new servlet and that deleted one have the same URL pattern. This was strange.

  1. Tried cleaning the Tomcat v10.0.23 server,
  2. Restarted the server,
  3. Build the exploded artifact,
  4. Restarted IDE.

Nothing works!!

Finally, I run the project in debug mode and that fix my issue.
This is strange but maybe I did something wrong that's why I faced this issue.

Hope this can help someone if they're facing the same issue.

Piyush Pranjal
  • 414
  • 4
  • 11