开发者

JSF - detect when user has exited browser window

开发者 https://www.devze.com 2023-02-07 06:52 出处:网络
In my application I have created some locking / unlocking functionality based on the current page that the user is viewing. A user can 开发者_开发知识库view a page and will acquire a lock which is sto

In my application I have created some locking / unlocking functionality based on the current page that the user is viewing. A user can 开发者_开发知识库view a page and will acquire a lock which is stored in my database, when they navigate away the lock is released and the database is updated.

I am trying to handle the situation when the user closes the browser window without logging out. They will still be holding the lock and I need that to be released and a database call needs to occur.

Is there a way to handle this in a JSF application using Spring or something else?

I know the javascript function onbeforeunload but I need to call a java method that will enable me to update the database.


The unload and beforeunload events aren't going to help you here since it's not by the specification guaranteed that any ajax request which is fired within those events is ever going to successfully hit the server. More than often, this won't.

The most reliable way is to keep the server side session lifetime very short, e.g. 1 minute and introduce an ajax poller on the page which polls within the same session to the server every minute (minus a few seconds) until the user is been inactive for more than the actual maximum session timeout. The user activity can be tested by listening on JavaScript keyboard (keyup) and mouse (click) events.


I am currently working on a two window application and needed to detect when a window is closed.

My JSF application uses ICEfaces. It provides an @WindowDisposed annotation for ViewScoped beans. With this annotation, the ViewScoped bean's @PreDestroy method will be called when the window is closed.

I have included an h:inputHidden tag in my page bound to the viewScoped bean and this works in terms of detecting the window closing and executing server side code.

0

精彩评论

暂无评论...
验证码 换一张
取 消