Question

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 ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top