如何检测浏览器关闭? [英] How to detect browser closing?

查看:170
本文介绍了如何检测浏览器关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网络应用程序中,当用户登录时,我将他的Id添加到servlet中的有效ID的向量中,当他退出时,我从向量中删除了他的Id,所以我可以看到有多少当前用户如果用户忘记注销,我的servelt生成的html包含:

In my web app, when a user logs in, I add his Id to a vector of valid Ids in the servlet, when he logs out, I remove his Id from the vector, so I can see how many current users are active, if a user forgets to log out, my servelt generated html has :

<meta http-equiv="Refresh" content="30; url=My_Servlet?User_Action=logout&User_Id=1111">

自动将他退出。

但我注意到许多用户永远都在那里,从未退出。我发现为什么,通过关闭他们的浏览器,他们从不手动或自动注销,因此他们的用户ID永远不会从有效的用户ID向量中删除。

But I've noticed many users are there for ever, never logged out. I found out why, by closing their browsers, they never manually or automatically logged out, so their user Ids will never be removed from the valid user Ids vector.

所以,我的问题是:我如何检测用户关闭他们的浏览器,所以我的servlet可以从向量中删除他们的ID?

So, my question is : how do I detect users closing their browsers, so my servlet can remove their Ids from the vector ?

我在隧道尽头看到一些亮点,但仍有问题,我的程序有这样的事情:

I see some light at the end of the tunnel, but there is still a problem, my program has something like this :

活跃用户列表:

User_1 : Machine_1 [ IP_1 address ]
User_2 : Machine_2 [ IP_2 address ]
User_3 : Machine_3 [ IP_3 address ]
...

我如何从会话监听器知道哪个用户的会话已经结束,因此将他从我的列表中删除?

How do I know, from the session listener, which user's session has ended and therefore remove him from my list?

我希望会话结束时,HttpServlet的 destroy()方法将是我可以在那里删除用户ID,但是当用户关闭浏览器时它永远不会被调用,为什么呢? HttpServlet中是否有其他方法在会话关闭时被调用?

I was hoping when the session ends, the HttpServlet's destroy() method would be called and I can remove the user Id in there, but it never gets called when user closes his browser, why? And is there any other method in the HttpServlet that gets called when a session closes?

推荐答案

没有办法知道服务器端(除非您使用一些JavaScript向服务器发送消息)浏览器已关闭。怎么会有?想想HTTP是如何工作的 - 一切都是请求和响应。

There is no way to know on the server-side (unless you are using some JavaScript to send a message to the server) that the browser has closed. How could there be? Think of how HTTP works - everything is request and response.

然而,应用服务器将跟踪Sessions何时处于活动状态,甚至会告诉您会话何时被销毁(比如由于超时)。请查看此页,了解如何配置 HttpSessionListener 接收这些活动。然后,您可以简单地跟踪活动会话的数量。

However, the application server will track when Sessions are active and will even tell you when a Session has been destroyed (such as due to time-out). Take a look at this page to see how to configure a HttpSessionListener to receive these events. Then you can simply keep track of the number of active sessions.

活动会话数将落后于当前用户的实际数量,因为在会话超时之前必须经过一段(可配置的)时间;但是,这应该有点接近(您可以降低会话超时以提高准确性)并且它比1)自己跟踪Sessions或2)在浏览器关闭时向服务器发送一些异步JavaScript更加清洁和简单(不保证会发送。)

The number of active sessions will lag behind the actual number of current users, since some period of (configurable) time has to elapse before a session is timed out; however, this should be somewhat close (you can lower the session-timeout to increase the accuracy) and it is a lot cleaner and easier than 1) tracking Sessions yourself or 2) sending some asynchronous JavaScript to the server when a browser is closed (which is not guaranteed to be sent).

这篇关于如何检测浏览器关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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