天天看点

iframe和response.sendRedirect()跳转到父页面的问题----已解决

在项目中,因为为了给页面分层次,就使用了 内嵌iframe 的分了三个框。在子页面进行操作的时候,如果session超时,就要被拦截器拦截重新回到首页进行登录,但是在sub页

面 ,进行操作的时候,如果session超时,需要跳转到首页进行登录的话,首页的页面就嵌在sub页面进行显示 了,这样显然是不符合逻辑了,应该是跳回到最顶层的父页面.

错误的代码如下:

HttpSession session = request.getSession();
		Object obj = session.getAttribute(Constant.LOGIN_USER);
		if (obj == null) {
			response.sendRedirect(request.getContextPath() + "/index.jsp");
			return false;
		}
           

因为response.sendRedirect()没有target属性,但html页面和js中有,于是,当判断出用户没有访问权限时,我们可以在jsp中使用js来转向到真正的登录页面。

正确跳转到父页面的代码:

HttpSession session = request.getSession();
		Object obj = session.getAttribute(Constant.LOGIN_USER);
		if (obj == null) {
		    PrintWriter out = response.getWriter();
		    out.println("<html>");    
		    out.println("<script>");    
		    out.println("window.open ('"+request.getContextPath()+"/index.html','_top')");    
		    out.println("</script>");    
		    out.println("</html>");  
			return false;
		}
           

继续阅读