• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HOW WILL I KNOW THAT THE BROWSER IS CLOSED?

 
Chhaya Dhanani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ALL,
I HAVE A SLIGHT PROBLEM IN MY APLICATION.
I WANT TO STORE THE USERIDS CURRENTLY IN SESSION. IF THEY TRY TO LOG IN AGGAIN, THEY SHUD BE DISPLAYED A MESAGE SAYING"THEY ARE ALREADY LOGGED IN". HOW DO I GO ABOUT DOING THIS?
ALSO, IF THEY LOG OUT, I AM INVALIDATING THEIR SESSION AND REMOVING THEIR USERID FROM THE LIST, BUT HOW WILL KNOW WHEN THE USER INSTEAD OF LOGGING OUT JUST CLOSES THE BROWSER WINDOW?
WILL HIS SESSION STILL BE RUNNING?
HOW CAN I INVALIDATE HIS SESSION WHEN HIS CLOSES HIS BROWSER WINDOW?
PLS REPLY ASAP... DEAD LINE IS THERE..
THANX IN ADVANCE
CHHAYA
 
Rick Hightower
Author
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

HI ALL,
I HAVE A SLIGHT PROBLEM IN MY APLICATION.
I WANT TO STORE THE USERIDS CURRENTLY IN SESSION. IF THEY TRY TO LOG IN AGGAIN, THEY SHUD BE DISPLAYED A MESAGE SAYING"THEY ARE ALREADY LOGGED IN". HOW DO I GO ABOUT DOING THIS?

Bad idea. You can do this. More details to come...

ALSO, IF THEY LOG OUT, I AM INVALIDATING THEIR SESSION AND REMOVING THEIR USERID FROM THE LIST, BUT HOW WILL KNOW WHEN THE USER INSTEAD OF LOGGING OUT JUST CLOSES THE BROWSER WINDOW?

You wont. The session will eventually time out. You can set the session time out to be low, but there is no way to know if they just closed their browser. Http is stateless.

WILL HIS SESSION STILL BE RUNNING?

Yes.

HOW CAN I INVALIDATE HIS SESSION WHEN HIS CLOSES HIS BROWSER WINDOW?

You can not. You can set the time out lower.

PLS REPLY ASAP... DEAD LINE IS THERE..

aye aye captain.
Now back to your original question.

I HAVE A SLIGHT PROBLEM IN MY APLICATION.
I WANT TO STORE THE USERIDS CURRENTLY IN SESSION.

No problem.

IF THEY TRY TO LOG IN AGGAIN, THEY SHUD BE DISPLAYED A MESAGE SAYING"THEY ARE ALREADY LOGGED IN". HOW DO I GO ABOUT DOING THIS?

Keep a list of users in a static hashmap or something. Look 'em up and see if they are already logged in.
I think this is a bad idea, but it is possible.
You could also log them out and invalidate their other orphanned session. (You could put the session in a hashmap keyed on the id... I've done it... it is possible).
You could also give the option of copying the state from their orphaned session to the current session, and this would accomplish what you want, but I think want you want may not really be what you really want.
See every time you exit your browser and reopen it you get a new session id. Why you ask? The jsessionid cookie that send the session with the header gets destroyed when the browser is closed... this is by design. Thus, if you do what you really want to do, then you would force them to wait until their session times out before they can use the system. This will make your users sad.
 
prabhat kumar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
implement HttpSessionBindingListener
and override valueunbound method to get notification of the session timeout/closure
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note, however, that the HttpSessionListener method(s) are not related in any way to the browser window closing. It just means that the user hasn't requested any resources from this webapp for a time longer than the timeout, or the session was invalidated by some action.
The browser window closing is strictly a client event .
You could try to program the client by stuffing javascript code to trap window close events and do something, but I think it's barking up the wrong tree.
 
Mujir Shaikh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides implementing Listener for the Object being kept in Session (which would clear the Object on Session Timeout), you can also catch Window Closing Event of Browser.
Though there is not direct way to catch this, what you can do is always submit thru a common script. Also keep a Javascript Variable for the Main Frame JSP. Set this Main Frame JSP Variable to 'y' in your individual JSPs before submitting each Form.
Catch window.unload Event for each Window and inside the function, check if the Variable is 'y'. If it isnt, it means
1) User has closed Browser Window,
2) User has types some other URL in Browser, in short hes leaving the site without logging out or
3) User has pressed Browser 'BACK' Button.
You can submit a Form to server with a particular hidden field set, and on Server-side check if this hidden value is set. If it is, you can clear Server Variables and invalidate the Session.
Hope that helps. You can contact me if you have some questions (bobby_oberai@hotmail.com)
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bobby:
This sounds very interesting and useful. Do you have this Javascript handy or you can provide a link or book that contains more detail?
Thanks.
 
Rick Hightower
Author
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby... sounds intersting. Cool beans!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic