File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Notification when user closes browser or leaves session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Notification when user closes browser or leaves session" Watch "Notification when user closes browser or leaves session" New topic

Notification when user closes browser or leaves session

Joe Guzzardo
Ranch Hand

Joined: Mar 14, 2000
Posts: 57
I'm using JSP for a web application. I want to use connection pooling. I need some way for the JSP/Servlet to tell me when the user has left my application or closed the browser so that I can free the connection. I created a servlet that is included with a JSP include directive. In the servlet I have destroy, init and doDelete methods defined. I'm using JRUN. When I close the browser I don't see any of these methods being called. Any ideas what I can do to get a notification when the session has been closed? TIA, Joe
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037

You can implement HttpSessionBindingListener in a class that will be notified when a session is destroyed.
What you appear to be doing - holding on to a connection for an entire session, is not what connection pooling is about. You should request a connect, do an operation and release the connection all in the course of a single request-response cycle. It is up to the connection pool to handle these operations efficiently.
init and destroy are called when the servlet object is first created and just before the servlet engine discards it.

author of:
Joe Guzzardo
Ranch Hand

Joined: Mar 14, 2000
Posts: 57
Hi Bill. Thanks for the advice. What you suggested is what I was planning on implementing initially but we wanted to keep the connection "alive" as long as the user was logged on to the application. We developed what I think is a rather elegant solution based upon the connection pooling example in the Core Servlets book by Marty Hall. We changed the "busy" vector to a HashMap using the session id as the key and the connection value and a timestamp as the data value. Then we just need to do a lookup on the session id to find the associated connection which we keep track of with a get/setAttribute. We also have a thread running to kill connections that have been idle for too long. I knew about the HttpSessionBindingListener
and plan on implementing that interface as well but for some reason my server is not expiring sessions.
I was really pleased that this was relatively easy to do within a servlet. Our application had been using JSP and beans exclusively. Maybe I could still do this with a bean but it seemed so much easier with a servlet. Sometimes I would feel that I just couldn't get my hands around a problem using JSPs and beans. But with a servlet I can really wrestle the beast to the ground. Bill, I really respect your opinions and appreciate your insights. Maybe you could open up a new thread to discuss the relative merits and strengths and weaknesses of JSPs and beans versus servlets. Personally, I think they all have their merits but judging when best to use one over the other is a real mark of professionalism. Thanks for your help. Regards, Joe
I agree. Here's the link:
subject: Notification when user closes browser or leaves session
It's not a secret anymore!