• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clear session on Browser Close

 
vicky Aggarwal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to javaranch
please check JavaRanch naming policy
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vicky Aggarwal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would also not possible ...
because flag is not reset
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will simply need to rely upon the session timeout. Period.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic