0

I have a backing session scope bean called WorkSession. When instance of this bean is created I call initialization function (declared with @PostConstruct annotation), which changes current user (I handle user logging with JASS) status in database to ONLINE.

I want to do analogical thing when user logs out (change user status to OFFLINE). It is easy to do this when user presses button "Logout" on web page. The problem is that i have no idea how to detect closed browser or tab in browser.

I see that method with annotation @PreDestroy does not work for this, because it's called by application server garbage collector, right?

I use Glassfish 3.1.2, JPA 2,0 and JSF 2.0.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Goofy
  • 4,637
  • 4
  • 34
  • 55
  • possible duplicate of [Java Servlet : How to detect browser closing?](http://stackoverflow.com/questions/299679/java-servlet-how-to-detect-browser-closing) – McDowell Apr 22 '12 at 21:29

1 Answers1

1

I see that method with annotation @PreDestroy does not work for this, because it's called by application server garbage collector, right?

It should work just fine. It's absolutely not called by the GC. It's called by the container when the session is destroyed. Perhaps your concrete problem is that you expected that the session is immediately destroyed when the user closes the entire browser. This is thus not true. It's only destroyed when it's timed out in the server side. The default timeout is 30 minutes. So if you wait 30 minutes, then the session will be destroyed and the @PreDestroy of all session (and view) scoped beans will be called.

You can configure the default timeout by <session-config><session-timeout> in web.xml.

See also:


By the way, the term "session disconnect" makes absolutely no sense in web development world.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452