Ajax submits session timeout and jumps to the page using the global method.

Source: Internet
Author: User

Write the following method to the filter:
Copy codeThe Code is as follows:
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httprequest = (HttpServletRequest) request;
HttpServletResponse httpresponse = (HttpServletResponse) response;
String url = httprequest. getRequestURL (). toString ();
If (httprequest. getSession () = null ){
If (httprequest. getHeader ("x-requested-")! = Null
& Httprequest. getHeader ("x-requested-with"). equals (
"XMLHttpRequest") {// ajax request
Httpresponse. setHeader ("sessionstatus", "timeout ");
} Else {
Httpresponse. sendRedirect ("/test/index. jsp ");
Return;
}
} Else {
Chain. doFilter (request, response );
}
}

In this way, if the session times out and is an ajax request, there will be a timeout in the Response Header;

Use a global method to process the page to jump to when the session times out.

Jquery can use the $. ajaxSetup method, and ext has a similar method:
Copy codeThe Code is as follows:
// Global ajax access. The sesion times out when ajax is cleared.
$. AjaxSetup ({
ContentType: "application/x-www-form-urlencoded; charset = UTF-8 ",
Complete: function (XMLHttpRequest, textStatus ){
Var sessionstatus = XMLHttpRequest. getResponseHeader ("sessionstatus"); // get the response header Through XMLHttpRequest, sessionstatus,
If (sessionstatus = "timeout "){
// If the request times out, specify the page to jump.
Window. location. replace ("/test/index. jsp ");
}
}
});

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.