您的位置:首页 > 其它

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: