Home >Web Front-end >JS Tutorial >Rewrite ajax to implement session timeout and jump to the login page example code

Rewrite ajax to implement session timeout and jump to the login page example code

韦小宝
韦小宝Original
2018-01-01 19:36:571224browse

This article mainly introduces the ajax example code for rewriting ajax to implement session timeout and jump to the login page. Friends who are interested in ajax can refer to the following

Question: Use window.location.href to jump Page, the backend only needs to implement a filter to redirect to the login page when the session times out. But what about using ajax? Using ajax to execute will result in a 302 error and it is impossible to jump to the page. Below I will post my front-end and back-end code to address this issue.

1, session filter


import java.io.IOException;
<p style="text-align: center"><img alt=""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;
import javax.servlet.http.HttpSession;
<p style="text-align: center"><img alt=""public class SessionFilter implements Filter {
<p style="text-align: center"><img alt="" public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
   throws IOException, ServletException {
  HttpServletRequest request = (HttpServletRequest) req;
  HttpServletResponse response = (HttpServletResponse) res;
<p style="text-align: center"><img alt=""  String requestUri = request.getRequestURI();
<p style="text-align: center"><img alt=""  if (requestUri.indexOf("/login.html") > 0 || requestUri.indexOf("/system/login") > 0) {
   return ;
  }
<p style="text-align: center"><img alt=""  HttpSession session = request.getSession(false);
<p style="text-align: center"><img alt=""  if (session == null) {
   // 如果是session超时,在此处做处理。
   response.sendRedirect(request.getContextPath() + "/login.html");
   return ;
  }
  try {
   filterChain.doFilter(request, response);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return ;
 }
}


2, web.xml Add configuration:


<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 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);
      //如果请求发生错误,会返回登陆页面源代码,如果源代码里面存在lovnx这个字符串,前端就重定向到登陆页面
      var reData = XMLHttpRequest.responseText + "";
      if(reData.indexOf(&#39;lovnx&#39;) != -1) {
       window.location.href="/manager/login.html" rel="external nofollow" ;
       return;
      }
     }
    });
    return _ajax(_opt);
   };
  });


## 4. Add code to the login page

<input type="hidden" value="lovnx">


The above is the example code that the editor introduces to you to rewrite ajax to implement session timeout and jump to the login page. I hope it will be helpful to everyone. ! !

Related recommendations:

Detailed explanation of ajax fileupload asynchronous upload plug-in

Detailed explanation of Ajax and PHP session creation shopping cart example

Detailed explanation of the simple implementation of AJAX paging effect

The above is the detailed content of Rewrite ajax to implement session timeout and jump to the login page example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn