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 Session id destroy when browser or tab is closed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session id destroy when browser or tab is closed" Watch "Session id destroy when browser or tab is closed" New topic
Author

Session id destroy when browser or tab is closed

sudarshan jadhav
Ranch Hand

Joined: May 28, 2012
Posts: 42
Hello everyone , I have a problem related to session handling in my code how to write a session invalidate method in browser tab closed or browser "X" closed event in servlet
and how to handling session timeout when session is timeout how session expire page is display in jsp when session is expire
Thank you in advance.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

HTTP is not a continuous-conversation protocol where the client connects to the server and they trade information asynchronously until the client disconnects.

Instead, each HTTP client request acts like it logs in, does one thing, then logs out. This request/response cycle repeats until the client stops sending requests.

At NO time is the server allowed to send a response to the client unless it's in response to a specific actual request and only one response is permitted per request.

So you cannot have a server post a "you timed out" page to the client. The closest you can get is to respond with a "you were timed out" response page if/when the client makes a request.

The other common thing people want to do is to notify the server that the client has closed a browser, window, or tab. There is no protocol defined for the World Wide Web to deal with that. If the user closes a server window/tab, the server is not notified by the client. Since there is no ongoing session to be dropped (remember, the connection only lasts long enough for the server to accept a request and return a response), there's no way the server will know that the client went away.


Customer surveys are for companies who didn't pay proper attention to begin with.
sudarshan jadhav
Ranch Hand

Joined: May 28, 2012
Posts: 42

Thank you "Tim" for reply,
please give me solution for following point:
1) Window close or tab close event in jsp & servlet.
2) When the session timeout tag is set in web.xml then display session expire page in jsp.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

sudarshan jadhav wrote: please give me solution for following point:
1) Window close or tab close event in jsp & servlet.
2) When the session timeout tag is set in web.xml then display session expire page in jsp.


Let me summarize Tim's answer for you:

You can't do either of those things. In other words, there is no solution.
sudarshan jadhav
Ranch Hand

Joined: May 28, 2012
Posts: 42

Thank you "paul" for reply,
But what is any other idea about the solution when the browser tab is closed or window is closed any java script is write for closing tab or browser???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

You simply need to rely upon the session timeout. The timeout could happen at any time and you cannot even know if the user is even looking at one of your pages when it occurs. It's likely they're off looking at videos of kittens at the time.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sudarshan jadhav
Ranch Hand

Joined: May 28, 2012
Posts: 42
Thanks all of you for reply,
My problem is that when user is login user related key is stored in hashmap entry of servlet context listener but when user side browser is closed then user related key is present in hashmap entry table how to remove this key from hashmap
please give me solution for this
Thank you in advance.............
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 829
    
  13

Check out the api for SessionListener. I've never used one, but it may be what you are looking for.

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
G krishna kumar
Greenhorn

Joined: Oct 10, 2012
Posts: 14
Here is the sample code how to use invalidate().

public void service(HttpServletRequest request,HttpServletResponse response) {


HttpSession session = request.getSession(false);

if(session != null) {

try {
session.invalidate();
PrintWriter out = response.getWriter();
out.println("<a href=\"/ls\">redirection to login screen </a>");

}catch(java.io.IOException e){

e.printStackTrace();
}

}


}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Session id destroy when browser or tab is closed
 
Similar Threads
Getting NullPointerException while counting number of visitors using session
Invalidate session when browser is closed
expired sessions
Sessions
Cookies