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("");
out.write("
out.write("alert('"+msg+"');");
out.write("top.location.href = '/EasyUI/login.jsp'; ");
out.write("");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}