Web Front-end JS Code:
$. Ajaxsetup ({contenttype: "application/X-WWW-form-urlencoded; charset = UTF-8", complete: function (xhr, textstatus) {If (xhr. status = 520) {// If the returned status code is 520 window. location .. reload (); // refresh the page and execute the logon logic return ;}}});
Java code:
1. Write a filter
Import Java. io. ioexception; import javax. servlet. filter; import javax. servlet. filterchain; import javax. servlet. filterconfig; import javax. servlet. servletexception; import javax. servlet. servletrequest; import javax. servlet. servletresponse; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class sessiontimeoutfilter implements filter {public void destroy (){} Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {httpservletrequest Req = (httpservletrequest) request; inclures = (httpservletresponse) response; // determine whether the session contains user information if (req. getsession (). getattribute ("username") = NULL) {// if there is an Ajax Request Response Header, X-requested-with; If (req. getheader ("X-requested-")! = NULL & req. getheader ("X-requested-"). inclusignorecase ("XMLHttpRequest") {res. setstatus (520); // indicates Session Timeout} else {chain. dofilter (req, Res) ;}} else {chain. dofilter (req, Res) ;}} public void Init (filterconfig chain) throws servletexception {}}
2. Add the filter in Web. xml
<filter> <filter-name>ajaxSessionTimeout</filter-name> <filter-class>org.tshark.framework.web.filter.SessionTimeoutFilter</filter-class></filter><filter-mapping> <filter-name>ajaxSessionTimeout</filter-name> <url-pattern>/*</url-pattern></filter-mapping>