• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get the servlet's requesting page ?

 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

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

Tks.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am unable to understand you point ?
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Thank you.

Regards, Jan
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic