========方法一========
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 }