0

In my web I'm using a servlet server. After I have processed my request, I would like to get back to the page that made the request. How do I do that?

I have used "response.sendRedirect(request.getParameter("url"));" but I'm getting a blank page instead of the page that made the request.

I will appreciate every help that will come.

user3312007
  • 63
  • 1
  • 1
  • 5

2 Answers2

0

You could use the refererHTTP header:

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
  String ref = request.getHeader("referer");
  response.sendRedirect(ref);
}

But keep in mind, that the refererheader is optional and some browser clients may not send it.

Udo Klimaschewski
  • 5,150
  • 1
  • 28
  • 41
0

You are transitioning in side your application. You can devise a strategy by which the page that is the source of the request is identified to the servlet.

It seems likely that there are many ways to do this. Here are a few:

  1. If the servlet is only responding to requests from a single page, hard code the return page in the servlet.
  2. Include a parameter in the request that lists the return page. For example <input type="hidden" name="returntopage" value="blammy.jsp"/>. This is not a great technique since it is easy to hack.
  3. Include a parameter in the request that identifies the return page. For example <input type="hidden" name="returntopage" value='${Blammy.getReturnToPageIdentifier("blammy.jsp")'> Presumably getReturnToPageIdentifier would access a map in the session and convert the page name into some identifier. The servlet would then take the identifier, reference the map, and get the page name. This is harder to hack because you can generate the identifier at load time (or for each session), thus the identifiers would change over time.
  4. Include a parameter in the request that identifies the source page. (similar to options 2 and 3 above but source page). The servlet can decide the page to which to transition based on the source page.
DwB
  • 37,124
  • 11
  • 56
  • 82