Java – servlet forward response caller / previous page

I tried to forward the servlet response to the same page it came from (a.k.a: Previous page, or "servlet" caller)

I've seen many answers (such as this and this), but I still can't make it work

I usually redirect the servlet's response to another page by doing the following:

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

But I tried to change "mynewpage" to another option I've seen:

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

And other options cannot make it work

What on earth did I do wrong?

Solution

First, request Getheader ("referer") returns a complete URL, but you must split http: / / server [: Port] /, because you pass it to request The contents of getrequestdispatcher () will be added to the application context. This:

/NameOfApp/http:/localhost:8084/NameOfApp/test.jsp

This is not what you want, because you only need to pass the following to the scheduler method:

test.jsp

If we take measures from the beginning, the first request starts from this URL:

http://localhost:8084/RequestDispatcher/test.jsp

Work forward, but the second time you have to make a request to your servlet, which will forward it to yourself So you will enter a servlet to call your own loop Why is that? Since you call the servlet from the form, this means that after the first request, the URL address in the browser address box will be changed to the URL address of the servlet

http://localhost:8084/RequestDispatcher/NewServlet

The servlet will forward the request back to the JSP page, and the browser will only display it, but the URL in the browser address box is still the URL containing the servlet instead of the JSP page:

http://localhost:8084/RequestDispatcher/NewServlet

So the next time you click Submit, the servlet will try to forward the request to itself If I were you, I would use redirection It seems better for your purpose:

response.sendRedirect(request.getHeader("referer"));

This will always change the URL in the browser address box and prevent servlet loops This will affect the request parameters, but you can always add them on the redirect URL (if it is not sensitive) or store them in the session until you retrieve them in the next first request (which will be requested by the redirect)

Frameworks like JSF can help you avoid these problems

The simplest solution that allows you to use forward is to keep the JSP (viewid) calling the servlet instead of using request Hidden parameters in the form of getheader ("referer") You need to check the loop because someone may deliberately change the value to force your servlet container to loop and eventually cause the VM to crash However, you can use a request attribute to record previous requests in the chain. If it is the same, you will reply with an error Therefore, in the servlet, you will use the hidden field value to determine where to forward to:

request.getRequestDispatcher(request.getParameter("viewid")).forward(request,response);

In your jsp:

<input type="hidden" name="viewid" value="test.jsp">

I think this will meet your requirements

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>