session过期,ajax请求处理
2017-12-18 00:00
363 查看
session会话过期,如果是请求。可以直接定位到页面。如果是ajax请求。无法跳转到页面。
可以通过如下方式实现,在Response的输出流里面,向前端写一段html代码来实现。
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
String url=request.getRequestURL().toString();
System.out.println("当前访问地址:"+url);
//登录页面不用检测,不然会出现Cannot forward after response has been committed(request多次提交)
if(url.indexOf("userLogin.do")>=0){
return true;
}
HttpSession session=request.getSession();
SessionInfo sessionInfo=(SessionInfo)session.getAttribute("sessionInfo");
if(sessionInfo!=null){
return true;
}
toAlert(response);
return false;
}
//前台弹出alert框
public void toAlert( HttpServletResponse response){
try {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
OutputStreamWriter out=new OutputStreamWriter(response.getOutputStream());
String msg="由于您长时间没有操作,session已过期,请重新登录!";
msg=new String(msg.getBytes("UTF-8"));
out.write("<meta http-equiv='Content-Type' content='text/html';charset='UTF-8'>");
out.write("<script>");
out.write("alert('"+msg+"');");
out.write("top.location.href = '/EasyUI/login.jsp'; ");
out.write("</script>");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
可以通过如下方式实现,在Response的输出流里面,向前端写一段html代码来实现。
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
String url=request.getRequestURL().toString();
System.out.println("当前访问地址:"+url);
//登录页面不用检测,不然会出现Cannot forward after response has been committed(request多次提交)
if(url.indexOf("userLogin.do")>=0){
return true;
}
HttpSession session=request.getSession();
SessionInfo sessionInfo=(SessionInfo)session.getAttribute("sessionInfo");
if(sessionInfo!=null){
return true;
}
toAlert(response);
return false;
}
//前台弹出alert框
public void toAlert( HttpServletResponse response){
try {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
OutputStreamWriter out=new OutputStreamWriter(response.getOutputStream());
String msg="由于您长时间没有操作,session已过期,请重新登录!";
msg=new String(msg.getBytes("UTF-8"));
out.write("<meta http-equiv='Content-Type' content='text/html';charset='UTF-8'>");
out.write("<script>");
out.write("alert('"+msg+"');");
out.write("top.location.href = '/EasyUI/login.jsp'; ");
out.write("</script>");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
相关文章推荐
- ajax前置处理实现异步请求session过期时跳转登录页面
- ajax请求时session已过期处理方案
- session过期,处理ajax请求,使其重新登录
- session失效时间设置、过期处理、ajax同步异步请求处理
- ajax请求 session过期跳转首页的两种处理方式
- js--Ajax的小知识(二):处理ajax的session过期的请求
- shiro 拦截器实现session过期拦截ajax请求的处理
- 处理jquery的ajax请求session过期跳转到登录页面
- flask session session已过期,再发送ajax请求如何处理?
- ajax请求 session过期跳转首页的两种处理方式
- Ajax 请求session过期的统一处理
- 当 jquery 发送 ajax 请求的时候遇到服务端session过期超时返回 302 跳转登陆页面的时候怎么办的处理方法
- Ajax请求session过期处理
- java Servlet Filter 拦截Ajax请求,统一处理session超时的问题
- mvc下ajax请求遇到session超时简单处理方式
- mvc下ajax请求遇到session超时简单处理方式
- session过期,使用Ajax每隔一段时间向服务器发送一次请求,来防止session过期
- ajax异步请求,session超时处理
- DWZ框架Ajax请求session超时处理
- 重写ajax方法实现异步请求session过期时跳转登录页面