JSP重定向:会话丢失问题
使用 response.sendRedirect(URL)替换了
< jsp:forward page =URL>
code>,我发现我知道丢失会话一旦重定向发生。有没有办法保留会话与重定向,或重新构建会话cookie并与重定向一起发送?
Having replaced a <jsp:forward page="URL">
with a response.sendRedirect("URL");
, I find I know lose the session once the redirect occurs. Is there a way to preserve the session with a redirect, or to reconstitute the session cookie and send it along with the redirect?
我知道我可以使用JavaScript通过
I know I could use JavaScript via window.location = "URL";
, but that is far from ideal!
任何帮助?
您不应该重新发送cookie,因为如果您建立了会话,cookie应该已经在客户端(浏览器)。
You should not have to resend the cookie, because if you had a session established, the cookie should already be on the client (the browser).
您确定要失去课程吗?你如何检测这个?在您重定向到的网址表示的页面中是否有新会话?
Are you sure you lose the session? How do you detect this? Do you have a new session in the page represented by the url you redirect to?
浏览器是否接受Cookie?否则你应该使用url重写。这是最有可能的原因。您可以尝试使用 HttpServletResponse.encodeRedirectURL
,然后将网址传递给 response.sendRedirect
。
Does the browser accept cookies? Otherwise you should use url rewriting. This is the most probable cause. You could try use HttpServletResponse.encodeRedirectURL
before passing the url to response.sendRedirect
.