Ajax请求Session超时解决
web前端js代码:
$.ajaxSetup({
contentType : "application/x-www-form-urlencoded;charset=utf-8",
complete : function(xhr, textStatus) {
if (xhr.status == 520) {//如果返回状态码是520
window.location..reload();//刷新页面,执行登录逻辑
return;
}
}
});
java代码:
1. 写一个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; public class SessionTimeoutFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
// 判断session里是否有用户信息
if (req.getSession().getAttribute("username") == null){
// 如果是ajax请求响应头会有,x-requested-with;
if (req.getHeader("x-requested-with") != null && req.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
res.setStatus(520);//表示session timeout
}else{
chain.doFilter(req, res);
}
}else{
chain.doFilter(req, res);
}
} public void init(FilterConfig chain) throws ServletException { }
}
2. 在web.xml中添加上面的filter
<filter>
<filter-name>ajaxSessionTimeout</filter-name>
<filter-class>org.tshark.framework.web.filter.SessionTimeoutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ajaxSessionTimeout</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Ajax请求Session超时解决的更多相关文章
- Ajax请求Session超时的解决办法:拦截器 + 封装jquery的post方法
目标:前端系统,后端系统等,统一处理Session超时和系统错误的问题. 可能需要处理的问题:Session超时.系统500错误.普通的业务错误.权限不足. 同步请求: Sess ...
- 【转载】Extjs设置Ajax请求的超时时间timeout
在Extjs中的Ajax请求中,Ext.Ajax.request 默认超时时间是30秒,有时候我们有比较耗时的操作需要设置更长时间,此时我们就需要修改Ext.Ajax.Requset的超时时间为更长, ...
- Shiro:ajax的session超时处理
本问题解决方案参照网站多篇文章融合解决,在此表示感谢! 环境:springboot+shiro+jquery-easyui 问题:在ajax请求时,如果此时session已经失效,系统没有自动跳转到登 ...
- ajax提交session超时跳转页面使用全局的方法来处理
来自:http://www.jb51.net/article/43770.htm 如果是ajax提交,超时时从服务器发出的跳转命令就不会起作用,所以如果是session超时,而且是在ajax请求,就在 ...
- 处理jquery的ajax请求session过期跳转到登录页面
首先需要在拦截器中判断是否是ajax请求,如果是 if(isAjaxRequest(request)){//ajax请求 response.setHeader("sessionstatus& ...
- ajax请求session失效重定向到登录页面
在ajax请求的页面引入一个自定义的AjaxRedirect.js的文件 AjaxRedirect.js的代码如下: $(function(){ $.ajaxSetup({ type: 'POST', ...
- Ajax 请求session过期的统一处理
public class LoginInterceptor extends HandlerInterceptorAdapter { @SuppressWarnings("unused&quo ...
- jquery ajax请求数据超时设置
var ajaxTimeoutTest = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'get', //请求方式,g ...
- ajax 请求登录超时跳转登录页解决方法
在Filter里判断是否登录,如果未登录返回401状态 public class SelfOnlyAttribute : ActionFilterAttribute { public override ...
随机推荐
- META标签的定义与使用(一、HTTP标题信息(http-equiv))
META标签分两大部分:HTTP标题信息(http-equiv)和页面描述信息(name). 一.http-equiv类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助正确和精确地显示网页 ...
- Benchmark of Large-scale Unconstrained Face Recognition-blufr 算法的理解
Many efforts have been made in recent years to tackle the unconstrained face recognition challenge. ...
- 【CSP-S/J 2019】初赛注意事项
UPD:10-25-13:33 正式成绩出了,省里500多名应该进了吧... UPD:10-20-10:07 现在又很慌啊,怎么感觉82又一点都不稳啊... 然后现在又不太想写文化课作业...我是不是 ...
- 数据库MySQL--常见基础命令
基础命令: 查看所有数据库:show databases; 打开指定的数据库:use 库名: 查看当前库的所有表:show tables; 查看数据库其他库中的表:show tables from 库 ...
- web Magic报错 NoSuchMethodError NoSuchMethodError: com.google.common.util.concurrent.SimpleTimeLimiter
webMagic使用selenium的时候遇到报错: java.lang.NoSuchMethodError: com.google.common.util.concurrent.SimpleTime ...
- RPC远程过程调用实例详解
1.创建IDL文件,定义接口. IDL文件可以由uuidgen.exe创建. 首先找到系统中uuidgen.exe的位置,如:C:\Program Files\Microsoft Visual Stu ...
- PAT甲级——A1114 Family Property【25】
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- 20.multi_case02
# 多进程,使用Process对象 from multiprocessing import Process def f(name): print('hello', name) if __name__ ...
- ssm 框架整合 代码初步 maven配置
pom.xml 配置<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <de ...
- 初识OpenCV-Python - 010: 精致边缘探测
本节主要介绍使用Canny函数达到边缘探测的结果. Code: import cv2from matplotlib import pyplot as plt img = cv2.imread('bal ...