This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. What is the semantic difference between / and /* in url-pattern of servlet-mapping? Is it any different?
 
Sagar Birari
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer.
I've just downloaded the specs and will read it ;-)
 
dexter morgan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64844
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic