关于在js中跳转还是在servlet中跳转

很多朋友喜欢在 js 中进行逻辑判断 , 然后跳转

优势很多 : 判断方便简捷 . 比如一个 session 超时的判断 .jsp js 源码如下 :

 

view plaincopy to clipboardprint?

    <mce:script type="text/javascript"><!--  

//session 过期判断   

function checkSession()  

{  

    if(<%=(request.getSession().getAttribute("user") == null) %>){   

        alert(" 对不起 , 登录已超时 , 请您重新登录 !");  

        top.location.href="/ 项目名 ";   

    }  

}  

 

  --></mce:script>   

      <mce:script type="text/javascript"><!--

              //session 过期判断

              function checkSession()

              {

                     if(<%=(request.getSession().getAttribute("user") == null) %>){

                            alert(" 对不起 , 登录已超时 , 请您重新登录 !");

                            top.location.href="/ 项目名 ";

                     }

              }

      

// --></mce:script>

 

他们认为使用了 struts actionServlet 就得配 findforward,top.location.href="/ 项目名 "; 语句就发挥不了作用了 , 以至于束手无策 , 只得又回到 jsp js 进行判断

其实 , 在很多情况下 , 使用 actionServlet 进行判断跳转 , 比使用 js 判断跳转要好 . 为什么 ? 抛却安全性等一系列原因 , 就多层架构分工来讲 , 就应该用 actionServlet 进行判断跳转 . 仍然拿那个判断 session 过期的问题 , 拿到一个继承了 DispatchAction actionServlet 类中判断源码如下

 

view plaincopy to clipboardprint?

    public ActionForward checkSession(ActionMapping mapping,  

             ActionForm form, HttpServletRequest request,  

            HttpServletResponse response) {  

             

              

        if(request.getSession().getAttribute("user") == null ) {  

            try {  

                PrintWriter out = response.getWriter();  

                out.write("<mce:script language=javascript><!--  

alert(' 对不起 , 登录已超时 , 请您重新登录 !');top.location.href='/ 项目名 ';   

// --></mce:script>");  

                out.close();  

            } catch (IOException e) {  

                 e.printStackTrace();  

                request.setAttribute("Error", e.getMessage());  

                return mapping.findForward("error");// 全局 error 处理页面   

            }  

        }  

        return null;  

    } 

 

 

 

本文来自 CSDN 博客,转载请标明出处: http://blog.csdn.net/defonds/archive/2009/05/06/4155550.aspx

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值