Session timeout processing for Ajax in WEB development __javascript

Source: Internet
Author: User
Tags java web

In Java Web Development, when the session timeout, the normal page of the jump good processing. The request for Ajax timeout processing, you need special treatment.

First write a unified filter, or interceptor, for the AJAX request filtering process, the following example to filter for example:

    public void Dofilter (ServletRequest request, servletresponse response,
            Filterchain chain) throws IOException, servletexception {
        httpservletrequest servletrequest= (httpservletrequest) request;
        HttpServletResponse servletresponse= (httpservletresponse) response;
        Determine if it is an AJAX request, because AJAX requests append x-requested-with=xmlhttprequest
        if ("XMLHttpRequest". Equalsignorecase ( Servletrequest.getheader ("X-requested-with"))) {
            Servletresponse.addheader ("Sessionstatus", "timeout");
        }       
        Subsequent code omitted ...
    }

Then when invoking Ajax on the page, the capture results are timed out as follows:

Global AJAX access, processing Ajax sesion Timeout
$.ajaxsetup ({
    type:post,
    contentType: "Application/json;charset=utf-8 ",
    ///Use complete capture results, do timeout processing
    complete:function (XMLHttpRequest, textstatus) {
        var data = Xmlhttprequest.responsetext;
        if (data = = "Timeout") {
            if (window.top!= window.self) {
                window.top.location = "${ PageContext.request.contextPath} ";}}}
);

Based on the Header to determine whether it is an AJAX request, if it is Ajax to throw out a status code.

This section of the processing of the JS code, can be extracted as a method, so easy to use in other places directly.

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.