In Java Web Development, when the session timeout, the normal page of the jump good processing. The request for Ajax timeout processing, you need special treatment.
First write a unified filter, or interceptor, for the AJAX request filtering process, the following example to filter for example:
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, servletexception {
httpservletrequest servletrequest= (httpservletrequest) request;
HttpServletResponse servletresponse= (httpservletresponse) response;
Determine if it is an AJAX request, because AJAX requests append x-requested-with=xmlhttprequest
if ("XMLHttpRequest". Equalsignorecase ( Servletrequest.getheader ("X-requested-with"))) {
Servletresponse.addheader ("Sessionstatus", "timeout");
}
Subsequent code omitted ...
}
Then when invoking Ajax on the page, the capture results are timed out as follows:
Global AJAX access, processing Ajax sesion Timeout
$.ajaxsetup ({
type:post,
contentType: "Application/json;charset=utf-8 ",
///Use complete capture results, do timeout processing
complete:function (XMLHttpRequest, textstatus) {
var data = Xmlhttprequest.responsetext;
if (data = = "Timeout") {
if (window.top!= window.self) {
window.top.location = "${ PageContext.request.contextPath} ";}}}
);
Based on the Header to determine whether it is an AJAX request, if it is Ajax to throw out a status code.
This section of the processing of the JS code, can be extracted as a method, so easy to use in other places directly.