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

在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 {
	}
}

js代码中,href链接根据实际情况使用相对路径。
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值