SRV.5.4 Request Path Elements


SRV.5.4 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 namespace, this path will be an empty string. Otherwise, this 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.

  • Path info : The part of the request path that is not part of the context path or the servlet path.

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 Table SRV.5-1.

Table SRV.5-1. Example Context Setup

Context Path

/catalog

Servlet Mapping

Pattern: /lawn

Servlet: LawnServlet

Servlet Mapping

Pattern: /garden

Servlet: GardenServlet

Servlet Mapping

Pattern: *.jsp

Servlet: JSPServlet

The behavior in Table SRV.5-2 is observed .

Table SRV.5-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



Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net