session過期 攔截器兩種處理方式返回登錄界面


========方法一========

 1 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
 2     //    /EasyUI/userLogin.do
 3     //String uri=request.getRequestURI();        
 4     
 5     //    http://localhost:8080/EasyUI/userLogin.do
 6     String url=request.getRequestURL().toString();
 7     
 8     System.out.println("當前訪問地址:"+url);
 9     
10     //登錄頁面不用檢測,不然再次使用跳轉會出現Cannot forward after response has been committed錯誤(request多次提交)
11     if(url.indexOf("userLogin.do")>=0){  
12         return true;  
13     }                  
14     HttpSession session=request.getSession();
15     SessionInfo sessionInfo=(SessionInfo)session.getAttribute("sessionInfo");
16     
17     if(sessionInfo!=null){
18         return true;
19     }        
20     
21     
22      //如果判斷是 AJAX 請求,直接設置為session超時
23     if( request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equals("XMLHttpRequest")  ) {
24         
25          response.setHeader("sessionstatus", "timeout");  
26          response.sendError(518, "session timeout.");  
27          
28     } else{
29         //不符合條件的,跳轉到登錄界面
30         request.getRequestDispatcher("/login.jsp").forward(request, response);
31     }    
32         
33     return false;
34 }

  另外在前端也是對ajax方法做處理,把代碼寫到自己的js里,在需要時引用該js

 1 /**
 2  * 設置未來(全局)的AJAX請求默認選項
 3  * 主要設置了AJAX請求遇到Session過期的情況
 4  */
 5 $.ajaxSetup({
 6     type: 'POST',
 7     complete: function(xhr,status) {
 8         var sessionStatus = xhr.getResponseHeader('sessionstatus');
 9         if(sessionStatus == 'timeout') {
10             var top = getTopWinow();
11             var yes = confirm('由於您長時間沒有操作, session已過期, 請重新登錄.');
12             if (yes) {
13                 /*location.href ---如果后面沒跟值 那么就是獲取當前頁面的url
14                     至於top, 表示是頂層頁面, 因為頁面之中可能嵌入了 frame 等子頁面,top表示最外面一層
15                     top.location.href  -- 當前頁面地址
16                  */
17                 top.location.href = '/EasyUI/login.jsp';            
18             }else{
19                 window.opener=null;
20                 window.open('','_self');
21                 window.close();
22             }
23         }
24     }
25 });
26 
27 /**
28  * 在頁面中任何嵌套層次的窗口中獲取頂層窗口
29  * @return 當前頁面的頂層窗口對象
30  */
31 function getTopWinow(){
32     var p = window;
33     while(p != p.parent){
34         p = p.parent;
35     }
36     return p;
37 }

 

========方法二========

java輸出流,在前端彈出alert提示后跳轉,只需要在java修改

 

 1 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
 2     //    /EasyUI/userLogin.do
 3     //String uri=request.getRequestURI();        
 4     
 5     //    http://localhost:8080/EasyUI/userLogin.do
 6     String url=request.getRequestURL().toString();
 7     
 8     System.out.println("當前訪問地址:"+url);
 9     
10     //登錄頁面不用檢測,不然會出現Cannot forward after response has been committed(request多次提交)
11     if(url.indexOf("userLogin.do")>=0){  
12         return true;  
13     }  
14         
15     HttpSession session=request.getSession();
16     SessionInfo sessionInfo=(SessionInfo)session.getAttribute("sessionInfo");
17     
18     if(sessionInfo!=null){
19         return true;
20     }
21     
22     toAlert(response);        
23         
24     return false;
25 }
26 
27 
28 
29 //前台彈出alert框
30 public void toAlert( HttpServletResponse response){
31        
32     try {
33          response.setContentType("text/html;charset=UTF-8");
34          response.setCharacterEncoding("UTF-8");
35             
36          OutputStreamWriter out=new OutputStreamWriter(response.getOutputStream());   
37          
38          String msg="由於您長時間沒有操作,session已過期,請重新登錄!";
39          msg=new String(msg.getBytes("UTF-8"));
40          
41          out.write("<meta http-equiv='Content-Type' content='text/html';charset='UTF-8'>");
42          out.write("<script>");
43          out.write("alert('"+msg+"');");
44          out.write("top.location.href = '/EasyUI/login.jsp'; ");
45          out.write("</script>");
46          out.flush();
47          out.close();
48 
49     } catch (IOException e) {
50         e.printStackTrace();
51     }
52 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM