This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to make user logout when user closes browser window? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to make user logout when user closes browser window?" Watch "How to make user logout when user closes browser window?" New topic
Author

How to make user logout when user closes browser window?

Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
I would like to know as to how i can make the user to logout automatically when the borwser window is closed or when the"BACK" button in browser is clicked.Is there any way of ensuring that the user doesn't view other pages without once the window is closed or when the "BACK" button is pressed <b>AFTER </b> login?
Sunetra Saha
Ranch Hand

Joined: Feb 25, 2001
Posts: 77
This has been discussed in this forum before, but I don't think a clear solution was offered. One way you can do this is by using Applets instead of jsps & servlets. But I guess we don't want that. You can try searching the archives though.
DAYANAND BURAMSHETTY
Ranch Hand

Joined: Aug 06, 2001
Posts: 34
Logout.jsp(Close the session)
----------
<%
session.invalidate();
%>

Dil se....,<BR>Dayanand<BR>0065-8839071(off)<BR>0065-7547034(Res0
HARSHAD bhawsar
Ranch Hand

Joined: Feb 22, 2001
Posts: 48
Hi,
See if this can help you?
Same problem was faced by me some time ago. But i sorted this problem by using a database table.
Create a temporary table and store some values like login , password , some random string and the system time.
When ever user call some other page or servlet it will check the time entered in the table. If the time in table is
older. Delete the row from the table and redirect user to login page.
You can delete all old timings from the table whenever some JSP or servlet is invoked.
Like some other person logs in the servlet or JSP will check all values of time, if they are old then it will delete them all.

You dont have specific solution to logout the user if the browser is closed or back button is pressed.
Harshad
DAYANAND BURAMSHETTY
Ranch Hand

Joined: Aug 06, 2001
Posts: 34
Logout.jsp(Close the session)
----------
<%
session.invalidate();
%>

And Every jsp/servlet U have to check the session.
If session is equal to null then go to the login page.


Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
right, I am using the invalidate() method and also am using the databse tables to check for the validity of user login.That is NOT a problem.What i am trying to check is this - the user logs in, browses/does something on the site(whatever) and then instead of loggin out he/she just closes the "x" button on the browser window.When this happens i want the user's session to end.(i.e to be logged out).I tried using Javascript for prompting the user to logout(by using onUnload() ).But in this case, even when the user is properly logged out, still the propmt appears asking the user to logout properly.How do i solve this problem?
Jack Wiesenthaler
Ranch Hand

Joined: Jul 26, 2001
Posts: 75
When dynamically generating the page, check the login state and generate the javascript accordingly...i.e. If the user is logged out then do not include the body onUnload() code.
I would just open a new window with the message "Logging out..." when processing the onUnload event and then close the window with in the new page's onLoad event. Pretty simple.
BTW, why the emphasis on logging the user out? The session will timeout anyway. Large pieces of information should be persistent (database) not stored in a session!
Mark Leong
Ranch Hand

Joined: Dec 11, 2000
Posts: 60
When the application starts, I had it open another window telling the user to log out by clicking a button. The user works on other part of the application and when everything is done, he click the logout window.
I know this does not prevent him from closing the window by clickig "x" but I guess its better than nothing.
Anyway, the session will timeout.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make user logout when user closes browser window?