關於在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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章