码迷,mamicode.com
首页 > Web开发 > 详细

对Ajax请求做Session过期判断

时间:2016-06-28 22:13:41      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 1 // 全局Ajax设置, 用于session过期判断
 2 function ajaxSetup() {
 3     $.ajaxSetup({
 4         timeout : 10000,
 5         beforeSend : function(xhr) {
 6             //添加ajax请求标识
 7             xhr.setRequestHeader("ajaxReq", "ajax");
 8         },
 9         complete : function(xhr, ts) {
10             if (xhr.statusText == ‘sessiontimeout‘ && xhr.status == 403) {
11                 // 跳转
12                 window.location.href = "login.jsp";
13             }
14         }
15     });
16 }

 

 1 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
 2 
 3         HttpServletRequest req = (HttpServletRequest) request;
 4         HttpServletResponse res = (HttpServletResponse) response;
 5         String servletPath = req.getServletPath();
 6         
 7         if(except.contains(req.getRequestURI())){
 8             chain.doFilter(request, response);
 9         }
11         
13         Object sessionObj = req.getSession().getAttribute(key);
14         if(sessionObj==null){
15             //如果是ajax请求
16             if(req.getHeader("ajaxReq")!=null && req.getHeader("ajaxReq").equals("ajax")){
17                 res.setHeader("sessionState", "timeout");
18                 res.setStatus(403);
19                 return;
20             }else{
21                 chain.doFilter(request, response);
22                 String contextPath = req.getContextPath();
23                 String redirect = servletPath + "?" + StringUtils.defaultString(req.getQueryString());
24                 res.sendRedirect(contextPath + forward + "?redirect=" + URLEncoder.encode(redirect, "UTF-8"));
25             }
26             
27         }else{
28             // pass the request along the filter chain
29             chain.doFilter(request, response);
30             System.out.println("chain.do after");
31         }
32         
33     }

 

对Ajax请求做Session过期判断

标签:

原文地址:http://www.cnblogs.com/luangeng/p/5625011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!