文章地址:http://m.blog.csdn.net/blog/Forrest_Chen/10344101
在Java web项目中写了个过滤器,用的页面是frameset,当点击页面中的某个链接,会传到后台,过滤器判断session值,如果session值为空则跳转到登录页面。由于用了框架,回到登录页面时候并不是整个窗口都跳转,而是链接所在的frame回到了登陆界面;如果刷新整个页面,会发现frameset中所有frame都回到登陆界面。
为了实现整个页面跳转,过滤器应该这么写,代码中do while中if一旦判断false跳出执行跳转,跳转使用javascript:
public class MyFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
HttpSession session = req.getSession(false);
do{
if(session == null) break;
Object id = session.getAttribute("id");
if( id == null || (Integer)id < 1) break;
//成功
chain.doFilter(request, response);
return;
}while(false);
//账户异常跳转,注意中文
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("<script language=\"javascript\">alert(\"您还没有登录,请先登录!\");if(window.opener==null){window.top.location.href=\"../login.jsp\";}else{window.opener.top.location.href=\"../login.jsp\";window.close();}</script>");
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}