如果用户关闭浏览器,如何自动结束会话 [英] How to end sessions automatically if user closes the browser

查看:875
本文介绍了如果用户关闭浏览器,如何自动结束会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在用户处于非活动状态一段时间后自动结束会话,比如10分钟。我们有一个方法

I need to end the session automatically when the user is inactive for some particular time say for 10 minutes. We have a method in

HttpSession session=request.getSession();
session.setAttribute("User", au);
session.setAttribute("name", firstname);
response.sendRedirect("doLogin.jsp");
session.setMaxInterval();

但即使用户有效10分钟,这也将结束会话。如何在用户关闭浏览器时结束会话?

but this will end the session even if the user is active for 10 minutes. How can I end the session when the user closes the browser?

推荐答案

请避免使用手动代码。

只需在 web.xml //这将适用于整个应用程序

<session-config>
<session-timeout>10</session-timeout>
</session-config>

检测浏览器关闭事件并致电 invalidate 方法

Detect browser close event and call invalidate method

if(session!=null) { 
session.invalidate();   
}   

这篇关于如果用户关闭浏览器,如何自动结束会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆