File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes HOW WILL I KNOW THAT THE BROWSER IS CLOSED? 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 » Servlets
Bookmark "HOW WILL I KNOW THAT THE BROWSER IS CLOSED?" Watch "HOW WILL I KNOW THAT THE BROWSER IS CLOSED?" New topic
Author

HOW WILL I KNOW THAT THE BROWSER IS CLOSED?

Chhaya Dhanani
Ranch Hand

Joined: Apr 11, 2001
Posts: 70
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

Joined: Feb 20, 2002
Posts: 350

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.


Rick Hightower is CTO of Mammatus which focuses on Cloud Computing, EC2, etc. Rick is invovled in Java CDI and Java EE as well. linkedin,twitter,blog
prabhat kumar
Ranch Hand

Joined: Apr 11, 2001
Posts: 114
implement HttpSessionBindingListener
and override valueunbound method to get notification of the session timeout/closure
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
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

Joined: May 18, 2002
Posts: 10
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

Joined: Sep 20, 2001
Posts: 666
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.


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Rick Hightower
Author
Ranch Hand

Joined: Feb 20, 2002
Posts: 350
Bobby... sounds intersting. Cool beans!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HOW WILL I KNOW THAT THE BROWSER IS CLOSED?
 
Similar Threads
How to track activesession
Clearing httpsession on window close, techniques?
please help me spring mvc
Session Invalidate -- Many ways
Restrict Login from Multiple Sessions