Servlet中response.sendRedirect()跳转时不能设置target的解决办法

一般使用Struts2的拦截器(或者是filter)验证是否登录的时候,如果用户没有登录则会跳转到登录的页面。这时候一般可以在拦截器或者filter中用response.sendRedirect()。

     但当在页面上使用了iframe后,发现跳转的只是页面中iframe内的区域,而父页面却没有跳转。拦截器或者过滤器中发送重定向请求时,是在iframe页面发送的。

 

原来的代码是这样的:

    public String intercept(ActionInvocation invocation) throws Exception {
        /* If the user have not been logined , it must return Login_Fail and forward to login.jsp */
        if( ActionContext.getContext().getSession().get("login_user") == null ) {
            HttpServletResponse response = ServletActionContext.getResponse();
            response.sendRedirect("login");
            return null;
        } else {
            return invocation.invoke();
        }
    }

因为response.sendRedirect()没有target属性,但html页面和js中有。于是,当判断出用户不具备访问权限时,可以在jsp中使用js来转向到真正的登录页面(这里我是采用的中间页面,但是我又不希望它显示出来,于是就要使用到JS了)。具体的代码可以这样写:

 

@Override

public String intercept(ActionInvocation invocation) throws Exception {

/* If the user have not been logined , it must return Login_Fail and forward to login.jsp */

   if( ActionContext.getContext().getSession().get("login_user") == null ) {

      HttpServletResponse response = ServletActionContext.getResponse();

      PrintWriter out = response.getWriter();

      out.println("<html>");  

      out.println("<script type=/"text/javascript/">");  

      out.println("window.open ('login','_top')");  

      out.println("</script>");  

      out.println("</html>");

      return null;

    } else {

      return invocation.invoke();

    }

}

 

 

OK!搞定!

 

转自:http://blog.csdn.net/allenlinrui/article/details/6422920

转载于:https://www.cnblogs.com/welcoming/p/3423118.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值