Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make user logout when user closes browser window?

 
Kameswari Jyosyula
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Logout.jsp(Close the session)
----------
<%
session.invalidate();
%>
 
HARSHAD bhawsar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic