您的位置:首页 > 编程语言 > Java开发

[转载]java servlet过滤器控制frame框架中的页面跳转

2015-09-24 21:54 519 查看
文章地址: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 {
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: