I was wondering, How would I do session.remove("userBean")
in JSF after user closes the window (hitting 'X' of browser) on JSF 1.2.
I was thinking to put a confirmation pop up after user hits 'X' of browser but not quite sure how do I access or bind the Ok
button to my defined method, where I have that logic to clear the scoped bean. Or is there any better approach of doing this?
Any help would be highly appreciated.
Thanks.
I was wondering, How would I do session.remove("userBean") in JSF after user closes the window (hitting 'X' of browser) on JSF 1.2.
You don't need to worry about it. If the session times out, then the bean will surely be removed. You can if necessary control the session timeout period in <session-config>
in web.xml
. If you actually want to hook on the session timeout/destroy, then you can just implement HttpSessionListener
and intercept on sessionDestroyed()
, or let UserBean
implement HttpSessionBindingListener
and intercept on valueUnbound()
.
If you really insist in listening on the browser close button, which is quite much more work after all, then all you can do is to use the Javascript's beforeunload
event. There are no other ways. You should only keep in mind that this is an unstandard event and that this is thus not per se supported by all webbrowsers. E.g. Opera doesn't support this event. It's quite a work to get this streamlined, because this event would also be fired when you do a plain vanilla navigation or a form submit, you'll need to suppress this event on every "normal" link/button. You'll also need to fire an Ajaxical request to inform the server about the close, because you cannot change the page location or submit a hidden form anymore as the browser is going to be closed.