加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

超时下Ajax请求处理

发布时间:2020-12-16 02:08:37 所属栏目:百科 来源:网络整理
导读:public class UserLogin extends HttpServlet{ /** serialVersionUID */ private static final long serialVersionUID = 464654165487455L; /** * 这里只是示例代码,没有考虑代码的合理性: * * The doGet method of the servlet. br * This method is called
public class UserLogin extends HttpServlet
{
    
    /** serialVersionUID */
    private static final long serialVersionUID = 464654165487455L;
    
    /**
     * 这里只是示例代码,没有考虑代码的合理性:
     * 
     * The doGet method of the servlet. <br>
     * This method is called when a form has its tag value method equals to get.
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException
    {
        // 如果是ajax请求,响应头会有x-requested-with相关字段信息
        if (null == request.getSession().getAttribute("user"))
        {
            if (request.getHeader("x-requested-with") != null
                && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest"))  
            {
                //在响应头设置session状态[可以在响应中自行添加相关字段信息]   
                response.setHeader("sessionstatus","nologin");
                return;
            }
        }
        
    }
    
    /**
    * The doPost method of the servlet. <br>
    *
    * This method is called when a form has its tag value method equals to post.
    * 
    * @param request the request send by the client to the server
    * @param response the response send by the server to the client
    * @throws ServletException if an error occurred
    * @throws IOException if an error occurred
    */
    public void doPost(HttpServletRequest request,IOException
    {
        this.doGet(request,response);
    }
    
}
  $.ajaxSetup({    
             contentType:"application/x-www-form-urlencoded;charset=utf-8",complete:function(XMLHttpRequest,textStatus){   
                    //通过XMLHttpRequest取得响应头,sessionstatus, 
                     var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus");    
                     if(sessionstatus=="nologin"){    
                                 //如果超时就处理 ,指定要跳转的页面   
                                 window.location.replace("${path}/common/login.do");    
                                }    
                     }    
            }    
           });  

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读