dcsimg
How to retrieve complete request URL from servlet
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   avdhut_nate
Posted On:   Friday, September 20, 2002 01:48 PM

Hi,

Can you pls tell me how do I retrieve Complete request URL from the servlet. I am using virtual URL...So I need to know the virtual URL from my servlet.

Is there any cookie or header...or some other parameter/method in the request object which wil solve my problem???


Thanks,
Avdhut.

Re: How to retrieve complete request URL from servlet

Posted By:   Laurent_Mihalkovic  
Posted On:   Friday, September 20, 2002 07:42 PM

Hi Avdhut,


The information is available, although not directly. The servlet specification has provision for accessing the original request as separated components. The following is the section of the spec you would be interested in:


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,
if the context is not rooted at the root of the server’s namespace, 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 the
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:

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

I always recommend that people read the spec as it is an invaluable source of information (concepts and practical details)

cheers
laurent

About | Sitemap | Contact