1

What is the difference between the two URL mappings : /* and / ?

<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DefaultServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

What I understood after reading the specs is that :

The pattern /* will force everything through MyServlet.
The pattern / will make DefaultServlet as the default servlet in the app .

Both almost means the same to me . Please let me know if there is any specific difference ?

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 1
    see [this](http://stackoverflow.com/questions/5276297/mapping-servlet-to-serve-my-requests), [this](http://www.coderanch.com/t/526534/Servlets/java/servlet-URL-mapping), and possibly [this](http://www.zomeon.com/3686420/servlet-filter-url-mapping). also, the [java servlet specs](https://jira.sakaiproject.org/secure/attachment/16135/servlet-2_4-fr-spec.pdf) may be of use. – Eliran Malka Feb 04 '13 at 10:01
  • 1
    @EliranMalka As a side note, you gave a link to the version 2.4 of the Servlet Specification, while the current one is version 3.0. Here is the link for [Java™ Servlet Specification Version 3.0](http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/) – informatik01 Feb 05 '13 at 16:27
  • thanks, @informatik01, that was for convenience sake - it was the only version i found exploded online for a quick reference, with no need to download. – Eliran Malka Feb 05 '13 at 22:49
  • @EliranMalka Oh, I see. Sorry about that )) – informatik01 Feb 05 '13 at 23:09

1 Answers1

0

Thanks for the links , going through them I have compiled this answer . Let us see a sample web.xml :

Case 1:

<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
`

In this case all requests /context/ , /context/anything and /context/sample.do will invoke servlet2 .

Case 2:

<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
`

In this case requests like /context/, /context/anything invokes servlet2 and /context/sample.do will invoke servlet1.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164