Rewrite ajax to implement session Timeout jump to the logon page instance code, ajaxsession
Problem: When you use window. location. href to jump to a page, the backend only needs to implement a filter to redirect to the login page when the session times out. But what about ajax? If ajax is used for execution, a 302 error occurs, and the page cannot be redirected. Next I will post my front-end code for this issue.
1. session Filter
Import java. io. IOException; <p style = "text-align: center"> 0 | requestUri. indexOf ("/system/login")> 0) {return;} <p style = "text-align: center">
2. Add configuration in web. xml:
<filter> <filter-name>sessionFilter</filter-name> <filter-class>com.manager.filter.SessionFilter</filter-class></filter><filter-mapping> <filter-name>sessionFilter</filter-name> <url-pattern>/manager/*</url-pattern></filter-mapping>
* 3. Rewrite ajax
Note: This code segment is placed on the index page.
JQuery (function ($) {var _ ajax = $. ajax; $. ajax = function (opt) {var _ success = opt & opt. success | function (a, B) {}; var _ opt = $. extend (opt, {success: function (data, textStatus) {_ success (data, textStatus) ;}, error: function (XMLHttpRequest, textStatus, errorThrown) {// alert (XMLHttpRequest. responseText); // if a request error occurs, the source code of the logon page will be returned. If the lovnx string exists in the source code, the frontend will redirect to the logon page var reData = XMLHttpRequest. responseText + ""; If (reData. indexOf ('lovnx ')! =-1) {window. location. href = "/manager/login.html" rel = "external nofollow"; return ;}}); return _ ajax (_ opt );};});
4. Add code on the login page
<input type="hidden" value="lovnx">
The above section describes how to rewrite ajax to implement session timeout and jump to the logon page instance code. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!