This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Clear session on Browser Close Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Clear session on Browser Close" Watch "Clear session on Browser Close" New topic
Author

Clear session on Browser Close

vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
Hi,
I am trying to implement a small application, in which i have some pre log out functionality.
Which i have implemented on log out link.
But Hw i can do same in case of browser close and session time out..
Thanks in adv.
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
On browser close I dont think it is possible,
beacuse http is stateless protocol,after sending response to browser(client) server did not khow client,
on next request server checks jsessionid if it is present then server going to know ohhh that is client we served before,
if jsessionid is null server going to create new session.
Then if we close browser we can not notify server.
this is I know correct me experts, if any way to do this.

but for session timeout you can use HttpSessionListener.


Hope this will help.


Thanks,
Shailesh
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Welcome to javaranch
please check JavaRanch naming policy
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

There is no way to really know how a window closes or if the refresh happens on browser window, unless and until if you have a reference for it (like by using window.open). Look for what Shailesh suggested using session timeout filter.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
Thanks for quick reply ...
But I m setting a flag in my database for user logged in.
Which i reset when user logout and .
so if user close browser without doing logout.
He can't log in again till previous session time out.
How i can implement desire functionality ...
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

vicky Aggarwal wrote:But I m setting a flag in my database for user logged in.
Which i reset when user logout and .
so if user close browser without doing logout.
He can't log in again till previous session time out.
How i can implement desire functionality ...

That's simple ! As you had flag set to 1 for the same user one who closed the browser, you just need to check the session associated with the coming request and if it has a valid session, you show the user "welcome page". Try this out.
vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
I don't think it will work ...
Because once i closed browser .. and i send a new request with new browser.
It won't have session Id.
My motive of using flag is to keep user restrict to sign in from single browser.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

vicky Aggarwal wrote:I don't think it will work ...
..and i send a new request with new browser.

Yeah that won't work, if you send request from new browser, because there would be no cookie to be sent from new browser. User need to sign in again !
vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
That would also not possible ...
because flag is not reset
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

But, on second loging in process from different browser, you can check that the user has already been signed in (by looking at flag) and you can (re)assigned that corresponding present session back to him. I never tried this idea, like re assigning session, but give it a try !
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
Try to have a look at HttpSessionListener, I think this might solve your problem (at least partially). Keep user id in session and when session is destroyed, simply set the flag to 0. But session is going to be destroyed after the specified timeout.
vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
Sagar Rohankar wrote:But, on second loging in process from different browser, you can check that the user has already been signed in (by looking at flag) and you can (re)assigned that corresponding present session back to him. I never tried this idea, like re assigning session, but give it a try !



In case of I don't know that first log in is using or not (Browser closed or not)
So i can't pass session to Second .. because my motive to keep one user log in on single place.
vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
Pavel Kubal wrote:Try to have a look at HttpSessionListener, I think this might solve your problem (at least partially). Keep user id in session and when session is destroyed, simply set the flag to 0. But session is going to be destroyed after the specified timeout.




Thanks a lot for your reply ...
Already implement your suggestion ....
Stuck on single point (browser close)
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

vicky Aggarwal wrote:Stuck on single point (browser close)

And you will be stuck there forever. Try searching "close browser window" in HTML - JavaScript forum, you will find experts comment on it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

You will simply need to rely upon the session timeout. Period.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clear session on Browser Close
 
Similar Threads
New session id in Jboss
Creating new session ids
how to implement globally unique identifier (GUID) in tomcat
Session Advice
javascript sending a request