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 }

 

转载于:https://www.cnblogs.com/Snowflakes-fall/p/7051130.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值