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

来源:互联网 发布:小缘鱼乐盛典 知乎 编辑:程序博客网 时间:2024/05/16 08:08

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

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

 

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:scripttype="text/javascript"><!--

              //session过期判断

              functioncheckSession()

              {

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

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

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

                     }

              }

      

// --></mce:script>

 

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

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

 

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:scriptlanguage=javascript><!--  

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

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

                out.close();  

           } catch (IOException e) {  

               e.printStackTrace();  

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

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

           }  

       }  

       return null;  

   } 

 

 

 

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

 

原创粉丝点击