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 ");
}
}
});