php ajax session死锁,session过期,ajax请求处理

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值