Servlet forward response to caller/previous page

Viewed 40294

I'm trying to forward a servlet response to the same page from when it came from (a.k.a.: the previous page, or the "servlet" caller).

I've seen many answers (like this and this) but still can't make it work.

I usually do the following to redirect the response of a servlet to another page:

request.getRequestDispatcher("MyNewPage").forward(request, response);

But I've tried to change "MyNewPage" for other options I've seen as solutions as:

request.getRequestDispatcher((String)request.getAttribute("javax.servlet.forward.request_uri")).forward(request, response);
request.getRequestDispatcher(request.getHeader("referer")).forward(request, response);

and other options and can't make it work.

What am I doing wrong?

3 Answers

I had the same problem and my solution was:

    String[] url = request.getHeader("referer").split("/");
    response.sendRedirect(url[url.length-1]);

Related