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 ...
随机推荐
- windows使用cmd查看、杀死进程
查看某个进程: netstat -ano | findstr 端口号 杀死某个进程: taskkill /f /pid 进程号
- linux 服务器安装mysql5.6
1.移除CentOS默认的mysql-libs: whereis mysql 2.为了避免冲突,先移除CenttOS上默认的mysql-libs: yum remove mysql-libs 3.然后 ...
- artTemplate(mark)
一个渲染性能出众模板引擎,无论在 NodeJS 还是在浏览器中都可以运行. 特性 拥有接近 JavaScript 渲染极限的的性能 调试友好:语法.运行时错误日志精确到模板所在行:支持在模板文件上打断 ...
- datetime模块练习
#_author:来童星#date:2019/12/6#1.获取当前日期import datetimeprint(datetime.date.today())# 2019-12-06#2.使用toda ...
- Redis中取出值,转成对象
import com.fasterxml.jackson.databind.ObjectMapper; //转成companyEntity CompanyEntity company = mapper ...
- 8 包含min函数的栈
0 引言 题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数.在该栈中,调用min.push及pop的时间复杂度都是O(). 1 抽象问题具体化 2 具体问题抽象分析 需要解 ...
- SpringBoot-application:application.yml/配置文件详解
ylbtech-SpringBoot-application:application.yml/配置文件详解 springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优 ...
- 编译报错 :The method list(String, Object[]) is ambiguous for the type BaseHibernateDao<M,PK>
原因:eclipse 的个bug,具体见http://stackoverflow.com/questions/10852923/method-is-ambiguous-for-the-type-but ...
- JAVA基础_反射获取泛型参数类型
我经常会想获取参数的实际类型,在Hibernate中就利用的这一点. domain: Person.java public class Person { // 编号 private Long id; ...
- Python第一课-Python的下载与安装
官网 https://www.python.org/ 我们安装的是windows 系统 Python3和Python2版本不兼容,我们下载最新的Python3.7.4 下载executatable版本 ...