• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

session and browser closure

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way to have it call valueUnbound once the browser closes??
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
reply
    Bookmark Topic Watch Topic
  • New Topic