aspose file tools*
The moose likes Servlets and the fly likes session and browser closure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "session and browser closure" Watch "session and browser closure" New topic
Author

session and browser closure

Chris Huisman
Greenhorn

Joined: Feb 12, 2001
Posts: 21
Hello,
I am having a problem unbinding my session when I close my browser. I use the HttpSessionBindingListener interfaces valueUnbound method to log out, thus terminating my session, but this does not work when I close my browser without logging off since the valueUnbound method must be called. I was wondering if there is a way to let the server side code know that the browser has shut down so I can clean up all of the objects bound to that session. I know the session object goes away when you quit your browser but my server side code needs to clean things up when the session goes away.
Any help is much appreciated.
Chris Huisman
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Originally posted by Chris Huisman:
Hello,

I am having a problem unbinding my session when I close my browser. I use the HttpSessionBindingListener interfaces valueUnbound method to log out, thus terminating my session, but this does not work when I close my browser without logging off since the valueUnbound method must be called. I was wondering if there is a way to let the server side code know that the browser has shut down so I can clean up all of the objects bound to that session. I know the session object goes away when you quit your browser but my server side code needs to clean things up when the session goes away.

Any help is much appreciated.

Chris Huisman


The sessions expire after a certain period of time (configurable in the web server or via the HttpSession.setMaxInactiveInterval(timelimit) ). And when it is expired by the servlet container, it does fire off a notification to your bounded objects.
Chris Huisman
Greenhorn

Joined: Feb 12, 2001
Posts: 21
Is there any way to have it call valueUnbound once the browser closes??
Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
dont beleive so.. remember that the browser has a stateless connection with the server.. the only way you can do this maybe if you have a client-side javascript that executes on close() of the browser (like those irritating ads that pop up AFTER you close your browser) calling a jsp / servlet that removes your sessions for you..
But why do this when the server will handle it automatically ?
in <head>
<script language="JavaScript">
//<!--
function closing(url,name) {
secondWindow=open(url, name, "width=350,height=400,status=no,toolbar=no,menubar=no");
}
//-->
</script>
</head>

<body onUnload="closing('../removesession.jsp','byebye')">
</body>

[This message has been edited by Mak Bhandari (edited April 24, 2001).]
[This message has been edited by Mak Bhandari (edited April 24, 2001).]
Raghav Sam
Ranch Hand

Joined: Apr 12, 2001
Posts: 412
you can invalidate any previous session before you actually create a new session like this:
HttpSession oldSession = request.getSession(false);
if (oldSession != null) {
oldSession.invalidate();
}
.
.
HttpSession newSession = request.getSession(true); //the first time you create a session like in a login page.
.
.
//do something.
this way even if your browser closes, while restarting the application, it will anyway check for any (old) session and invalidate it.
hope this helps,
------------------
Raghav.
[This message has been edited by Raghav Sam (edited April 26, 2001).]


Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.<br />- Dr. Seuss
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Raghav Sam:[B]
you can invalidate any previous session before you actually create a new session like this:
[/B]

This is only a viable option if you have something like an entrance page in the first place, and won't work if you jump straight to a page in the middle of the site.
When push comes to shove, there is AFAIK no 100% surefire way to invalidate the session when the user leaves your site. You can try and do smart things with JavaScript and entrance pages, but ultimately it's a limitation imposed by the statelessness of the HTTP protocol.
- Peter

[This message has been edited by Peter den Haan (edited April 26, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session and browser closure
 
Similar Threads
SessionListener?
Session Management Still weird
Browser close session destroy
Free resources when session times out
Session Advice