cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Deployment of java servlet fails when changing url pattern in web.xml

0 Kudos
1,998

Hi community,

I'm currently playing around with Java servlets on HCP and I'm not overly familiar with that.

Currently, I have this piece of config in my web.xml:

  <servlet-mapping>

  <servlet-name>HelloWorldServlet</servlet-name>

  <url-pattern>/*</url-pattern>

  </servlet-mapping>

This works perfectly fine and my app is properly deployed. However, when I change the url pattern to something else (e.g. to / or /hello-world/*), the service remains in Starting state.

There's the following log entry:

Failed to start web application at context path '/HelloWorld'

org.eclipse.gemini.web.core.spi.ServletContainerException: Web application at '/HelloWorld' cannot be added to the host.

  at org.eclipse.gemini.web.tomcat.internal.TomcatServletContainer.startWebApplication(TomcatServletContainer.java:128)

   [...]

I can't really make sense of it. Does anyone have an idea about what might cause this?

Thanks,

Max

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello,

There should be more information in the logs. Can you please share the whole exception not only part of it.

Regards,

Violeta

0 Kudos

Indeed, there's something interesting hidden deep in the logs: (full log attached)

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

But I still cannot make sense of it.

Here's the web.xml (omitted some resource-ref's and init-param's but that shouldn't make a difference)

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

  version="3.0">

  <display-name>helloworld</display-name>

  <servlet>

        <servlet-name>MyODataSampleServlet</servlet-name>

        <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>

        <!-- more config -->

    </servlet>

  <servlet>

  <servlet-name>HelloWorldServlet</servlet-name>

  <servlet-class>helloworld.HelloWorldServlet</servlet-class>

  </servlet>

  <servlet-mapping>

  <servlet-name>MyODataSampleServlet</servlet-name>

  <url-pattern>/*</url-pattern>

  </servlet-mapping>

</web-app>

Again, when I change the url-pattern from /* to /, I get the above error.

Thanks in advance!

- Max

Former Member
0 Kudos

Hi,

The exception says the following:

you have servlet with name X in the web.xml which is mapped to a concrete url pattern.

and you have again servlet with name Y annotated with @WebServlet which is mapped to the same url pattern.

Please note that when you do not specify a name in @WebServlet then the Web Container will use as servlet name the fully qualified class name i.e. package + classname

For Web Container servlet name matters so you may end up with one and the same servlet class with different names (the first specified in the web.xml and the second - default from annotation - fully qualified class name) - from Web Container point of view these are different servlets.

So please ensure that you are using either web.xml or annotations.

Regards,

Violeta

0 Kudos

I see, know it also makes sense that Eclipse didn't generate a web.xml file in the first place

Thank you so much!

Answers (1)

Answers (1)

gowrisankar_m2
Product and Topic Expert
Product and Topic Expert
0 Kudos

I guess you have to change in the servlet as well.[url pattern which you using it..]