many url-pattern for the same servlet
Asked Answered
C

2

42

I need to map the same servlet on two different url. I used netbeans 7.0.1 for managing my whole project, so I used its friendly interface to modify the web.xml file. What netbeans created is this:

<servlet-mapping>
    <servlet-name>fred</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>/url</url-pattern>
</servlet-mapping>

This is read by tomcat 5.5 without emitting any error, but only the second pattern works, while the first one is ignored.

Googling I found that the right way for tomcat is this one:

<servlet-mapping>
    <servlet-name>fred</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>fred</servlet-name>
    <url-pattern>/url</url-pattern>
</servlet-mapping>

So, my questions: is this a bug in tomcat? What syntax do other containers accept?

Criseyde answered 24/1, 2012 at 22:42 Comment(0)
E
51

I guess it has more to do with the servlet spec the container/netbeans is using rather than being an issue with the container. Your net beans seems to be using the spec 2.5 to construct the servlet mapping and hence you get

<servlet-mapping>
   <servlet-name>fred</servlet-name>
   <url-pattern>*.jsp</url-pattern>
   <url-pattern>/url</url-pattern>
</servlet-mapping>

Read more about this here. It says

Previous versions of the servlet schema allows only a single url-pattern in a filter mapping.For filters mapped to multiple URLs this results in needless repetition of whole mapping clauses.

Egger answered 25/1, 2012 at 5:30 Comment(4)
I would say that tomcat should at least give an error if it is using the old spec (2.4) and doesn't accept the new syntax. Or netbeans should use the old syntax in order to have a compatible web.xml. Isn't it? Does new containers accept the old syntax with many servlet-mapping stanzas?Criseyde
@Criseyde I'm pretty sure most of them are backward compatible :) and the 2.5 servlet spec XSD certainly allows that .Egger
@Criseyde If you are satisfied with the answer you could accept it be clicking the tick mark next to the question and/or you could up vote the answer if this has helped you in any way . Accepting answers will enhance your reputation in this forum .Egger
I better checked all my settings. It happens that I have all tomcat servers in my netbeans, i.e., tomcat 5.5, tomcat 6 and tomcat 7. But I cannot select tomcat 5.5 as server for testing my application. It seems that my project have a property called Java EE 5, so it wont work with container that implements servlet 2.4. This is because Java EE 5 imply Servlet 2.5. So I am inclined to think that web.xml created by netbeans must only satisfy Servlet 2.5. In this case I think the only bug is in Tomcat 5. I'll report it to tomcat authors.Criseyde
A
1

Tomcat container will support 3 url patterns:

  1. complete character sequence
  2. /*
  3. *. ext (star means anything)

/* is recommended for only one single framework

if you use multiple framework then recommended .*

Alfieri answered 14/9, 2016 at 7:16 Comment(1)
URL pattern #2 can be /somestring/* where somestring can include / characters. A fourth pattern, / on its own, specifies the default servlet of the application. Servlet Specification 3.1 supports a fifth pattern using "" (empty string). Check the appropriate Servlet Specification for your servlet version under topic Specification of Mappings for details.Berry

© 2022 - 2024 — McMap. All rights reserved.