Thursday, 7 February 2013

Request Path Elements | Java Servlets Tutorial pdf

Request Path Elements

The request path that leads to a servlet servicing a request is composed of many important sections. The following elements are obtained from the request URI path and exposed via the request object:
=> Context Path: The path prefix associated with the ServletContext that this servlet is a part of. If this context is the “default” context rooted at the base of the Web server’s URL name space, this path will be an empty string. Otherwise, if the context is not rooted at the root of the server’s name space, the path starts with a’/’ character but does not end with a’/’ character.
=> Servlet Path: The path section that directly corresponds to the mapping which activated this request. This path starts with a’/’ character except in the case where the request is matched with the ‘/*’ pattern, in which case it is an empty string.
=> PathInfo: The part of the request path that is not part of the Context Path or the Servlet Path. It is either null if there is no extra path, or is a string with a leading ‘/’.
The following methods exist in the HttpServletRequest interface to access this information:
=> getContextPath
=> getServletPath
=> getPathInfo
It is important to note that, except for URL encoding differences between the request URI and the path parts, the following equation is always true:
requestURI = contextPath + servletPath + pathInfo
To give a few examples to clarify the above points, consider the following:
Table 1: Example Context Set Up
Context Path                         /catalog
Servlet Mapping                     Pattern: /lawn/*
                                              Servlet: LawnServlet
Servlet Mapping                     Pattern: /garden/*
                                              Servlet: GardenServlet
Servlet Mapping                     Pattern: *.jsp
                                              Servlet: JSPServlet
The following behavior is observed:
Table 2: Observed Path Element Behavior
Request Path                                                     Path Elements
/catalog/lawn/index.html                                  ContextPath: /catalog
                                                                      ServletPath: /lawn
                                                                      PathInfo: /index.html
/catalog/garden/implements/                            ContextPath: /catalog
                                                                      ServletPath: /garden
                                                                      PathInfo: /implements/
/catalog/help/feedback.jsp                              ContextPath: /catalog
                                                                      ServletPath: /help/feedback.jsp
                                                                      PathInfo: null

No comments: