我们如何在浏览器关闭事件中调用注销servlet

问题描述:

如何处理浏览器的close事件以触发注销?

How can I handle the close event of a browser to trigger a logout?

前一段时间,我一直在寻找一个一致的解决方案,而我的决定是在不相信浏览器或用户的情况下处理这种情况.

I've searched for a consistent solution to this problem some time ago, and my decision was to handle the situation without believing the browser or the user.

您无法确定用户是否会离开您的页面以及何时离开,即使您使用"onunload",在浏览器关闭或离开页面之前也可能不会收到该事件.浏览器崩溃不会给您任何信号,因此相信您知道用户仍在浏览您的页面并可能采取任何进一步措施是有缺陷的.

You can't for sure know if a user is leaving your page and when, and even if you use 'onunload', you may not get the event before the browser closes or leaves your page. And browser crashes won't give you any kind of signal, so believing you know the user is still viewing you page and may take any further action is flawed.

关于注销用户的最佳措施是设置合适的会话超时,如果需要进一步控制,则可以尝试在具有我还活着"信号的关键页面上添加AJAX调用,然后注销用户不再响应(需要启用AJAX/JS的浏览器,并可能排除潜在用户).

The best you can do about logging out an user is put a fitting session timeout, and if you need further control, you may try adding AJAX calls on critical pages with 'I'm alive' signals, and then logging out users that are not responding anymore (that would require AJAX/JS enabled browser and may exclude potential users).