Home Web Front-end JS Tutorial Rewrite ajax to implement session timeout and jump to the login page (graphic tutorial)

Rewrite ajax to implement session timeout and jump to the login page (graphic tutorial)

May 22, 2018 pm 03:38 PM
ajax session time out

This article mainly introduces the example code of rewriting ajax to implement session timeout to jump to the login page. Friends who need it can refer to the following

Question: When using window.location.href to jump to the page, The backend only needs to implement a filter to redirect to the login page if 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 ;
 }
}
Copy after login

2. Add configuration to 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>
Copy after login

*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);
   };
  });
Copy after login

4. Add code to the login page

<input type="hidden" value="lovnx">
Copy after login

The above is I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

Native JS implements Ajax cross-domain request flask response content (graphic tutorial)

Ajax implements website hijacking Detection method

Ajax front-end and back-end cross-domain request processing method (graphic tutorial)

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

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

How does Meituan pay for overtime? Meituan's overtime compensation standards! How does Meituan pay for overtime? Meituan's overtime compensation standards! Mar 16, 2024 pm 07:55 PM

1. How will Meituan compensate for overtime? Meituan’s overtime compensation standards! Meituan’s overtime compensation rules are as follows: (1) Overtime when purchasing the Punctual Service: After selecting the Punctual Service, if the delivery rider fails to deliver on time, the system will automatically start the compensation process, and the amount of compensation will be determined based on the order details and the overtime duration. . (2) Ordinary timeout for non-purchased punctual products: 1. If the actual delivery time of the order is more than 10 minutes but less than 20 minutes later than the promised delivery time, 25% of the actual payment amount of the order will be compensated. 2. If the actual delivery time of the order is more than 20 minutes or less than 30 minutes later than the promised delivery time, 30% of the actual payment amount of the order will be compensated. 3. If the actual delivery time of the order is more than 30 minutes later than the promised delivery time, 50% of the actual payment amount of the order will be compensated. 4

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

What to do if Meituan's errand delivery times out_How to deal with Meituan's errand delivery timeout What to do if Meituan's errand delivery times out_How to deal with Meituan's errand delivery timeout Mar 28, 2024 am 09:26 AM

1. First of all, when taking out food, you need to know whether the order is delivered by the merchant itself or by Meituan. Generally speaking, the order receiving efficiency of the merchant's self-delivery is low and timeouts often occur. However, since Meituan is not involved in the delivery, there is no timeout. Compensation principle. At this time, you can check to see if the order submitted contains a compensation clause for overtime delivery. If there is a relevant clause in the claim, there is no need to say more, the merchant will claim the claim. If there are no relevant rules, it is recommended that you leave a negative review or leave a message about the meal delivery service on the platform, or contact the merchant directly to complain about the delivery service so as to negotiate compensation. If you really can't negotiate, you can only admit that you are out of luck. Please pay more attention next time. 2. Overtime compensation model: The merchant promises a delivery time and a discount, and receives payment from the user

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

Asynchronous data exchange using Ajax functions Asynchronous data exchange using Ajax functions Jan 26, 2024 am 09:41 AM

How to use Ajax functions to achieve asynchronous data interaction With the development of the Internet and Web technology, data interaction between the front end and the back end has become very important. Traditional data interaction methods, such as page refresh and form submission, can no longer meet user needs. Ajax (Asynchronous JavaScript and XML) has become an important tool for asynchronous data interaction. Ajax enables the web to use JavaScript and the XMLHttpRequest object

See all articles