wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes How to get the servlet's requesting page ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "How to get the servlet Watch "How to get the servlet New topic
Author

How to get the servlet's requesting page ?

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi guys,

How to get the page URL which posted some data to a servlet from within that servlet ?

Tks.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
saqib sarwar
Ranch Hand

Joined: Mar 30, 2007
Posts: 77
i am unable to understand you point ?


SCJP5 95%, SCWCD 85%<br />Knowledge is the Life of Mind
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

When your servlet's doGet(), doPost(), ... method is called, you get a parameter of type HttpServletRequest.

One of it's methods, getRequestURL() profides this functionality:
Reconstructs the URL the client used to make the request.

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Thank you very much for your prompt reply.

Let me clear some more my doubt :

First : Supposing my jsp page located at http://MyApp/product/showDetails.jsp have a button which calls the servlet named UpdateProduct mapped to url /product/save.do

Second : From within that servlet I would like to know some method that returns the URL that made the servlet call, in this case, the url /product/showDetails.jsp

I know there are some methods in the request such as getServletPath, getQueryString and so on ... but by using these methods I get the url /product/save.do which is not what I want.

Dear Jan,

Cany you confirm whether the getRequestURL you posted above suits for my needs ?

Thanks a lot.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

whether the getRequestURL you posted above suits for my needs ?
No it doesn't. I interpreted your initial question differently.

I don't know if the Servlet API has a solution for this (except for the unreliable request.getHeader("referer") ) -- or "Referer", I'm not sure. .
You could add the name of the form to the form's submit url:
and retrieve that in your servlet (request.getParameter("myPage"))

Regards, Jan

[ May 09, 2007: Message edited by: Jan Cumps ]
[ May 09, 2007: Message edited by: Jan Cumps ]
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Thanks again Jan for your prompt reply.

I think your suggestion could be considered for sure.

Anyway, I solved my problem by using a filter which intercepts each user request and then tracks the url as a session attribute by using contextPath(), servletPath() and pathInfo() methods right before calling the doFilter method. This way I think it's more transparent for the web designer just because he/she doesn't need to bother on remembering to inform a request parameter on each posted url as you suggested above.

Anyway, thank you very much for your prompt suggestion.

Later.
[ May 09, 2007: Message edited by: Edisandro Bessa ]
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

I'm trying to understand your solution. Doesn't that give you /product/save.do?

(I'm referring to this thread,
The spec says:

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.
* PathInfo: The part of the request path that is not part of the Context Path or
the Servlet Path.
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
At a first shot the answer seems yes. But think with me...

Once the filter is executed before the jsp call, then I can track the requested URL properly.

So, as I do not want filter calls to Servlets and I need just filter calls for JSP pages, when calling the Servlet thru save.do link, no filter will be fired and then the Servlet will be able to read the last accessed url tracked when the user requested the JSP page.

Did you get the point ?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

Yes. Thank you.

Regards, Jan
venu gopalK
Greenhorn

Joined: Jul 23, 2007
Posts: 1
Dear Edisandro,
I too have the same problem what you have faced getting requesting page URL in the servlet. I cant understand your solution. So can you send me sample code snippet to understand it better.


Thanks in Advance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get the servlet's requesting page ?
 
Similar Threads
Session get value of 0 is not displaying
sendRedirect?
Doubt with JSTL
How do I get the last page served up?
how session/servlet/application can get pageContext?