Friday 25 January 2013

Textual Representation of logo

Servlet Request URL Pattern Explained

Every servlet is mapped with url pattern in web.xml . Container tries to find the appropriate Servlet using following rules :
  •  it will try to find the web application context root by matching the request url . it will match longest web application path . 
  •  after finding the web application context root , it will try to match the remaining part of url with the      available pattern in following order , any successful match will stop further matching                                                             

  1. it will try to find a exact match
  2. it will recursively match the longest path where path separator is '/'. if a path matching pattern is provided. 
  3. if url contains some extension , it will try to match extension pattern.
  4. if no pattern is matched , it will go to default servlet , if defined by web application .

a path matching pattern is a url pattern which starts from / and end with '*' .

if a servlet is mapped with url pattern '/' then it is called the default servlet 

if a servlet is mapped with empty string url pattern then it is mapped to context root of web application

HttpServletRequest Provide two methods for accessing url provided by user :

getServletPath()  provides servlet path.

getPathInfo()  provides excess path info if path matching pattern is used in finding the servlet.


now let's take examples to understand the process :
<servlet-mapping>
    <servlet-name>ExactMatchServlet</servlet-name>
    <url-pattern>/abc</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>DefaultServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>PathInfoServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>LongPathInfoServlet</servlet-name>
    <url-pattern>/abc/*</url-pattern>
  </servlet-mapping>
if a request is /abc it will match to ExactMatchServlet
 here Servlet Path is /abc whereas path info is null

 if a request is /abc/pqr , it will match to LongPathInfoServlet instead of PathInfoServlet because this path matched is longer .
 here Servlet Path is /abc and path info will be /pqr .

 if a request is /something/random , it will match to PathInfoServlet
 here ServletPath is empty string whereas path info will be /something/random

if we change the PathInfoServlet's url mapping from /* to /something
and then , a request is made  /something/random , then it will match to DefaultServlet .
here ServletPath is /something/random and path info is null .