How can I solve the problem of Ajax request session failure?

Source: Internet
Author: User

How can I solve the problem of Ajax request session failure?

In general, all our projects have logon filters. Generally, requests are sufficient. However, AJAX is an exception, so the solution is to set the response to session failure.

The configuration is divided into two parts: Filter and page JS. First, let's look at the modification of the 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; import javax. servlet. http. httpSession;/*** logon filter * determines whether the Session is invalid and whether the user logs on. * If the request is ajax, the session timeout is set * @ author Merlin. ma **/public class LoginFilter implements Filter {private String redirectUrl = "/login.html"; private String sessionKey = "userName"; @ Override public void destroy () {}@ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) r Equest; HttpServletResponse rep = (HttpServletResponse) response; HttpSession session = req. getSession (); if (session = null | session. getAttribute (sessionKey) = null) {// if it is an AJAX request, set it to session Timeout if (req. getHeader ("x-requested-")! = Null & req. getHeader ("x-requested-"). equals ("XMLHttpRequest") {rep. setHeader ("sessionstatus", "timeout");} else {rep. sendRedirect (req. getContextPath () + redirectUrl);} else {chain. doFilter (request, response) ;}@override public void init (FilterConfig filterConfig) throws ServletException {String url = filterConfig. getInitParameter ("redirectUrl"); String key = filterConfig. getInit Parameter ("sessionKey"); redirectUrl = url = null? RedirectUrl: url; sessionKey = key = null? SessionKey: key ;}}

The code is simple, so I just need to comment out more. Now I want to see the JS Code. Of course, it is based on jQuery ~~

// Global ajax access. When ajax is cleared, the sesion times out $. ajaxSetup ({contentType: "application/x-www-form-urlencoded; charset = UTF-8", complete: function (XMLHttpRequest, textStatus) {var sessionstatus = XMLHttpRequest. getResponseHeader ("sessionstatus"); // get the response header Through XMLHttpRequest, sessionstatus, if (sessionstatus = "timeout") {// process the response if it times out, specifies the page window to jump. location. replace ("login.html ");}}});

Load the JavaScript code and call ajax. If you do not log on or the session fails, the logon page is displayed.

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.