Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher.forward() - reload/refresh page link? / how to get original URL?

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My servlets (Servlet1, Servlet2, etc.) use RequestDispatchers so they may forward their requests to ErrorServlet.

In ErrorServlet, I want to output a 'Retry' link, that provides a link to the original servlet (Servlet1 or Servlet2, etc.), but how do I obtain the href value of the original servlet for such link.

I've tried request.getRequestURL() but it gives the ErrorServlet url, so how would I get the original Servlet1/Servlet2/etc url from within ErrorServlet? (Is the only way to add the url as a session attribute or is there some simpler way?)

I need to achieve this without resorting to javascript.

Many thanks,
James
 
Monika Joshi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
In the servlet, that does the forwarding to the ErrorServlet, set a request attribute which keeps the original path i.e request.setAttribute("OriginalScreenPath", request.setServletPath()); and then get the request dispatcher and forward to ErrorServlet.Now in the ErrorServlet, request is coming in as parameter.This request will contain not only the original request parameters, but also the path to Original servlet("OriginalScreenPath") that you set before forwarding the request. This you can now use when you generate the response and with Retry link.

Cheers,
Minakshi
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for your reply. So, to recreate the original link with params, I'd use it like this?...

String originalScreenPath = (String)request.getAttribute("originalScreenPath");
String params = request.getQueryString();
String href = originalScreenPath + "?" + params;

Cheers,
James
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic