request.getHeader(“referer”), than back button reload the page

Deadly 提交于 2019-12-12 04:04:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!