一般使用filter过滤用户是否登录,如果用户没有登陆则转向登陆页面,这时候可以使用response.sendRedirect()。
但当在页面上使用了iframe后,发现被重定向的只是父页面中的iframe区域,登陆页面内容显示在该区域中。说明在过滤器中发送重定向请求时,是在iframe页面发送的。
- 一般写法
//session 超时
if(currentUser == null) {
// 未登录
response.sendRedirect("/login/toLogin");
return false;
}
- 使用iframe
//session 超时
if(currentUser == null) {
// 未登录
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<script>");
out.println("window.open ('/login/toLogin','_top')");
out.println("</script>");
out.println("</html>");
return false;
}
使用 response.getWriter() 该方法可以将字符文本发送到浏览器。
关于 window.open ('/login/toLogin','_top')