java Servlet 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7612681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Servlet redirect
提问by TGM
resp.sendRedirect("/myurl");
req.getSession().setAttribute("foo", "bar");
In this case, do I have access to the foo attribute after the redirect? On generally speaking, a servlet is completely executed before the redirect is made or it stops it's execution after the redirect line?
在这种情况下,我可以在重定向后访问 foo 属性吗?一般来说,servlet 是在重定向之前完全执行还是在重定向行之后停止执行?
Thanks
谢谢
回答by Dave Newton
It continues execution.
它继续执行。
It's not a return
, it just adds information to the response.
它不是return
,它只是向响应添加信息。
回答by chakri
after redirecting to that particular page, the control goes to that page and come back to old page and execute the req.getSession().setAttribute("foo", "bar"); also. this is the sendRedirect() bahaviour
重定向到该特定页面后,控件转到该页面并返回旧页面并执行 req.getSession().setAttribute("foo", "bar"); 还。这是 sendRedirect() 行为
回答by Dirk
I found out a more generic approach that works for jsp files as well as for servlets.
我发现了一种适用于 jsp 文件和 servlet 的更通用的方法。
String url = "http://google.com";
response.reset();
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader("Location",url);
response.getWriter().close();
response.getWriter().flush();