wood burning stoves 2.0*
The moose likes Servlets and the fly likes servlet-mapping / url-pattern: / vs /* Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet-mapping / url-pattern: / vs /*" Watch "servlet-mapping / url-pattern: / vs /*" New topic
Author

servlet-mapping / url-pattern: / vs /*

Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Hi. What is the semantic difference between / and /* in url-pattern of servlet-mapping? Is it any different?
Sagar Birari
Greenhorn

Joined: Sep 05, 2008
Posts: 11
The answer is in the servlet spec:

quote:
SRV.11.2 Specification of Mappings

In theWeb application deployment descriptor, the following syntax is used to define mappings:

* A string beginning with a / character and ending with a /* suffix is used for path mapping.
* A string beginning with a *. prefix is used as an extension mapping.
* A string containing only the / character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
* All other strings are used for exact matches only.



The pattern /* will force everything through your servlet.
The pattern / will make your servlet the default servlet for the app, meaning it will pick up every pattern that doesn't have another exact match.

Regards


Sagar Birari
Development & Technical Blog
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Thank you for your answer.
I've just downloaded the specs and will read it ;-)
dexter morgan
Greenhorn

Joined: May 16, 2010
Posts: 1
After getting this wrong like 2 dozen times over the last ten years, I want to say this post is probably the best answer *except* that the spec has one other provision that is relevant. JSPs are implicitly mapped to the "*.jsp" mapping. So, if you use the mapping that puts everything through your servlet (/*), you basically can't use JSPs (cuz any time you forward or redirect to a JSP, the request is going to boomerang back to your servlet).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

This also applies to other resources such as images, stylesheets and scripts. It's rarely useful to map /*.

Also, dexter morgan, please use real words when posting to the forums and avoid fake words such as "cuz". Thanks.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet-mapping / url-pattern: / vs /*