问题
JSP1 links to JSP2.
JSP2 call a servlet that does some stuff and ends with:
response.sendRedirect(request.getHeader("referer"));
return;
At this point I'm back to JSP2. The problem is that pressing the browser back button from there, the page simply reload itself instead of going back to JSP1. This is correct becouse the sendRedirect adds an entry to the history.
But I want to go back to JSP1. Any tips?
回答1:
You shouldn't rely on the referrer header in controller actions at all for various sensitive reasons. You've already found out at least one. This header isn't always been sent by the client or its value may not actually represent the real referrer. Some browser, proxy and even anti-virus configurations may hide, change or even obfuscate the referrer header. Use it at highest for statistical purposes.
Rather pass the from
as a request parameter instead,
<input type="hidden" name="from" value="${pageContext.request.requestURI}" />
with this logic (validation omitted)
response.sendRedirect(request.getParameter("from"));
Or, better, if it's always the one and same JSP based on other request parameters, just hardcode/configure it fully in the server side.
来源:https://stackoverflow.com/questions/10791109/request-getheaderreferer-than-back-button-reload-the-page