Monday, 1 November 2010

URL Patterns in servlet-mapping by examples

Example of working servlet mapping in web.xml
<servlet-mapping>
<servlet-name>MySerlvet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
All URL patterns must begin with / (slash).

Case 1 : Match URL path /api/*
http://gigadot.net/mywebapp/api Matched
http://gigadot.net/mywebapp/api?query=document Matched
http://gigadot.net/mywebapp/api/resource Matched
http://gigadot.net/mywebapp/api/resource/file.txt Matched
http://gigadot.net/mywebapp/directory/api Does not Matched

Note :
/api/*/path is not a valid url-pattern. /* can only be present once at the end of url-pattern.
/* will match anything after the context path and send request to matching servlet.

Case 2 : Match extension *.action
http://gigadot.net/mywebapp/api.action Matched
http://gigadot.net/mywebapp/dictionaty/api.action Matched
http://gigadot.net/mywebapp/resources/files.action?query=item Matched
http://gigadot.net/mywebapp/api.resource.action Matched
http://gigadot.net/mywebapp/directory/api.ACTION Does notMatched
http://gigadot.net/mywebapp/directory/api.actionist Does notMatched


Note:
/api/*.action is a valid url-pattern.

Case 3 : Default servlet /
It matches every request if no other url-pattern defined in web.xml matches. The servlet mapped to this pattern is called the default servlet. It is used explicitly to ensure that any malformed URL is handled rather than throwing an error.

No comments:

Post a Comment