Ajax requests in Extjs automatically jump when the session fails

Source: Internet
Author: User

Ajax requests in Extjs automatically jump when the session fails
When the session fails, we can use filter to intercept and redirect to the login page. However, if the request is an ajax request, the page will not be redirected because ajax returns data and cannot return the page.

Unlike other requests, ajax asynchronous requests include X-Requested-With: XMLHttpRequest in the request header. See the differences between the two figures below.

We can use the filter to determine whether a request is asynchronous. If yes, we can return a sessionstatus. On the page, we can determine whether the session is invalid by judging the sessionstatus value.

The first is a common request:

 

The second one is ajax

 

However, if every ajax is written like this, the workload is huge.
Because I use Extjs and Ext ajax is a Singleton, we can write a separate js file and write the code for session Timeout verification in it, you only need to introduce this js for other pages.

The code below is filter:

 

Import java. io. IOException; import java. io. printWriter; 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; public class SessionFilter implements Filter {public void destroy () {} public void doFilter (ServletRequest request, response, FilterChain chain) throws IOException, ServletException {HttpServletRequest httpRequest = (HttpServletRequest) request; httpServletResponse httpResponse = (HttpServletResponse) response; HttpSession session = httpRequest. getSession (); String url = httpRequest. getRequ EstURI (); String path = url. substring (url. lastIndexOf ("/"); if (check (path) & session. getAttribute ("user") = null) {// intercept ajax if (httpRequest. getHeader ("x-requested-")! = Null & httpRequest. getHeader ("x-requested-"). equalsIgnoreCase ("XMLHttpRequest") {httpResponse. addHeader ("sessionstatus", "timeout"); chain. doFilter (request, response); // not required; otherwise, an error occurs in the request.} else {String str = "<script language = 'javascript '> alert ('session expired, please log on again '); "+" window. top. location. href = index. jsp '"+"'; </script> "; response. setContentType ("text/html; charset = UTF-8"); // solve Chinese garbled try {PrintWriter Writer = response. getWriter (); writer. write (str); writer. flush (); writer. close ();} catch (Exception e) {e. printStackTrace () ;}} else {chain. doFilter (request, response) ;}}/*** determine whether to intercept the path: true [Yes] * @ param path * @ return */public boolean check (String path) {if (path. indexOf ("login ")! =-1 | path. indexOf ("logout ")! =-1) // do not intercept login logout return false; return true ;}@ Override public void init (FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub }}

The configuration in web. xml is as follows:

 

 

         
  
   sessionFilter
          
  
   com.web.helper.session.SessionFilter
  
 
         
  
   sessionFilter
          
  
   /*
  
 
The following is the code for ajax timeout verification [ajax_timeout.js]. After this js is introduced to the page, this code is called every ajax request.

 

 

// Ajax callback function to process session expiration Ext. ajax. on ('requestcomplete', checkUserSessionStatus, this); function checkUserSessionStatus (conn, response, options) {var status = response. getResponseHeader ("sessionstatus"); // Ext re-encapsulates the response object if (status = "timeout") {window. location. href = "index. jsp ";}}

---------------------------------------- ---------------

---------------------------------------- -------------------

Later I found that if the code still uses form's submit submission for upload, the return value of this submit cannot be received because ajax and submit return values are different:

 

We have to do different processing:

 

// Ajax callback function to process session expiration Ext. ajax. on ('requestcomplete', checkUserSessionStatus, this); function checkUserSessionStatus (conn, response, options) {lele.info (response); console.info (response. status); if (response. status = undefined) {// solves the problem of Form submission not responding} else {var status = response. getResponseHeader ("sessionstatus"); // Ext re-encapsulates the response object if (status = "timeout") {window. location. href = "index. jsp ";}}}


 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.