The moose likes Servlets and the fly likes how does the servlet knows that the client has closed the browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how does the servlet knows that the client has closed the browser" Watch "how does the servlet knows that the client has closed the browser" New topic

how does the servlet knows that the client has closed the browser

Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
I want the servlet to know that the user has closed the browser. If the user logs out by clicking on log-out button in the page, the servlets can know that the user has logged out(and can invalidate the session). But if the browser is closed by the user without logging out, how can the servlet know that the user has logged out? or in other words i want the session to invalidated whenever the user logs out(which I can do) or whenever the user closes the browser(which the servlet is not able to know).
The servlet maintains a list of users logged in.
Please let me know the solution.
thanks in advance

Author of JPhotoBrush Pro (
ashish kulkarni
Ranch Hand

Joined: Aug 15, 2002
Posts: 131
The servlet will never come to know about the browser closing event, may u can pu tsome javascript to check for browser closing and then report it to the servlet...

Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
if i report it to the servlet(by javascript calling the servlet with certain parameters on unload event of the browser), the request will also come back and a new browser opens up to display the response of the servlet.(The original window will however close) .
I don't want the new window to be opened. I guess if u send a request ( I guess any reporting to the servlet is a request) to the servlet, a response will come back to the same browser window. If theat browser window is closed by this time, a new browser window opens up.
So can you or anyone else help me. I don't want the response to come back (which forces the browser to open a new window).
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

The solution is to put a timeout on the login session.
Browsers have no way to reliably notify the server, and in any event, if the Browser or client OS crashed (which was fairly common until recently), there would be cases where notification couldn't be relied on anyway.
A lot of websites do provide a "logout" button, but as a user, I can tell you that I often forget it's there and just move onto another website.
Which is another reason why you can't get a good notification. If I jump to a new website OR open another browser window to a new website, the session to the old website is still there - HTTP isn't a connect-to-only-one-server type of service.

An IDE is no substitute for an Intelligent Developer.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
I like to use a class that implements HttpSessionBindingListener - if you attach one of these to the user's session, it will get called when the session is deleted after it times out - or - if you call the session invalidate method. That way, no matter what happens your servlet eventually gets notified that the user is not active.
There are also other session listeners that some folks prefer to use.
Ken Robinson
Ranch Hand

Joined: Jul 25, 2002
Posts: 52
By nature, a web server forgets about the user as soon as the current request has been satisfied. Only through the Session API can the next request from that user be identified. Although a Logout button can be utilized, it is rarely (as mentioned above) used.
If you are using version 2.3 of the JSDK, you can use LifeCycleEvents. Most Web Containers allow you to set a Session Timeout, which invalidates a Session after so much time as passed. By utilizing an HttpSessionListener, you can guarentee you will be notified a Session is about to be destroyed.
While this approach still requires you wait for the Session to time out, it does give you the ability to guarentee the Session is invalidated as you would like it.
I would suggest any resource that can be shared across multiple users/request (Database connections) be used via a pool (DataSource) whenever possible.
I agree. Here's the link:
subject: how does the servlet knows that the client has closed the browser
It's not a secret anymore!