Spring MVC session timeout, processing AJAX requests

Source: Internet
Author: User

When doing web development, when the session expires, if it is not an AJAX request, it is easy to jump to the specified page. But there are problems with Ajax requests. When the session expires, clicking on the AJAX request will pop up some page source files.

First, an interceptor was built to determine the session timeout. User Login will save user information in a session, in the session of the listener, session timeout will destroy the user information stored in the session, and the interceptor through the session whether there is user information to determine the session timeout.

  interceptor is the SPRING-MVC interceptor, which in the interceptor determines whether it is an AJAX request:

public class logininterceptor extends handlerinterceptoradapter{public boolean  Prehandle (httpservletrequest request,               httpservletresponse response, object handler)  throws Exception  {             object obj =  request.getsession (). getattribute (Securityconstants.login_user);           if  (null == obj)  { //not logged in          if  (Request.getheader ("X-requested-with")  != null &&  Request.getheader ("X-requested-with"). Equalsignorecase ("XMLHttpRequest")) { //If an AJAX request response header will be available, x-requested-with               Response.setheader ("Sessionstatus",  "timeout");//In response header setting sEssion Status           }else{         response.sendredirect (Request.getcontextpath () + "/account/login");         }          return false;         }        return  Super.prehandle (Request, response, handler);}}

Thus, if the session times out and is an AJAX request, it will be in the response header, Sessionstatus has a timeout;

Then use a global method to process the session timeout to jump to the page.

jquery  can use the $.ajaxsetup  method, ext also has a similar method

   $.ajaxsetup ({          contenttype: " Application/x-www-form-urlencoded;charset=utf-8 ",           Complete:function (xmlhttprequest,textstatus) {          var  sessionstatus=xmlhttprequest.getresponseheader ("Sessionstatus"),  //obtains the response head through XMLHttpRequest, Sessionstatus,           if (sessionstatus== "Timeout") {           alert ("Login timeout, please login again!")          //If the &nbsp is processed when timed out, specify the page to jump to               window.location.replace (webPath.webRoot +  " Account/login ");            }              }        }); 



Spring MVC session timeout, processing AJAX requests

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.