[ACCEPTED]-Servlet forward response to caller/previous page-servlet-3.0
First of all the request.getHeader("referer")
returns a full URL but 48 you have to strip off the http://server[:port]/
because what 47 you pass to request.getRequestDispatcher() is 46 added to the application context like this:
/NameOfApp/http:/localhost:8084/NameOfApp/test.jsp
Which 45 is not what you want because you just need 44 to pass the following to the dispatcher 43 method:
test.jsp
If we take things from the start 42 the first request starts from this URL:
http://localhost:8084/RequestDispatcher/test.jsp
The 41 forward works but the second time you are 40 going make a request to your Servlet, the 39 Servlet will forward to itself. So you will 38 enter a loop with the Servlet calling itself. Why 37 will this happen? Since you call the Servlet 36 from the form, this means that the URL address 35 in your browser address box will change 34 to that of your Servlet after the first 33 request.
http://localhost:8084/RequestDispatcher/NewServlet
The Servlet will forward the request 32 back to the JSP page and the browser will 31 display just that but the URL in the browser 30 address box will still be the one containing 29 the Servlet and not the JSP page:
http://localhost:8084/RequestDispatcher/NewServlet
So the 28 next time you hit submit the Servlet will 27 try to forward the request to itself. If 26 I were you I would use a redirect. It seems 25 more appropriate for your purpose:
response.sendRedirect(request.getHeader("referer"));
This will 24 always change the URL in your browser address 23 box and prevent your Servlet from looping. This 22 will have an impact on the request parameters 21 but you can always add them on the redirect 20 URL(if it's not sensitive information) or 19 store them in session until you retrieve 18 them on the next first request which will 17 be made by the redirect.
A framework like 16 JSF would save you from these issues.
The 15 easiest solution that would allow you to 14 use forward, is a hidden parameter in the 13 form keeping the JSP(viewid) that called 12 the Servlet instead of using the request.getHeader("referer"). You 11 will need to check for loops though, because 10 someone could deliberately change the value 9 to force your Servlet container to loop 8 and eventually crash the VM. But you can 7 just use a request attribute to keep record 6 of the previous request in the chain and 5 if it's the same you respond with an error. So 4 in the servlet you would use the hidden 3 field value in order to decide where to 2 forward to:
request.getRequestDispatcher(request.getParameter("viewid")).forward(request, response);
and in your JSP:
<input type="hidden" name="viewid" value="test.jsp">
I think this 1 would cover your requirements.
Better strategy would be to add the caller 5 attribute in the request. So that after 4 processing the callee servlet can forward 3 its request back to the caller.
Something 2 like,
Caller
request.setAttribute("caller", "/page/Foo.jsp");
Callee
String uri = (String)request.getAttribute("caller");
getServletContext().getRequestDispatcher(uri).forward(request, response);
OR you can try adding the caller in 1 the session and remove it after forwarding.
I had the same problem and my solution was:
String[] url = request.getHeader("referer").split("/");
response.sendRedirect(url[url.length-1]);
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.