Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java:Why http session is not destroyed when tab or browser is closed?

Tags:

java

session

I have the following implementation of HttpSessionlistener

public class SessionListener implements HttpSessionAttributeListener, HttpSessionListener {


public void attributeAdded(HttpSessionBindingEvent event) {
   ...

}


public void attributeRemoved(HttpSessionBindingEvent event) {

    ...
}

public void attributeReplaced(HttpSessionBindingEvent event) {

}

//HttpSesion creation & destruction
public void sessionCreated(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    //log created time


}

public void sessionDestroyed(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    long destroyedTime = System.currentTimeMillis();
    //log destroyed time

}

}

Basically i log the session creation and destruction time. But if the session is long (default to 30 minutes), and user closes browser meanwhile, the

sessionDestroyed 

is not called ?

Why is that ? Is there a workaround to log exactly the when the session was destroyed (when user closes the browser)? Should'nt be this be the browser's problem, to kill the session when it is closed ?

Is there any interface that i have to implement for this to work ?

Thank you !

like image 600
Flueras Bogdan Avatar asked Apr 01 '09 15:04

Flueras Bogdan


People also ask

How does HTTP session work in Java?

The Servlet HTTP session uses a cookie with the name JSESSIONID and a value that identifies the session. The Servlet container keeps a map (YMMV) of HttpSession objects and these identifiers. When a client first makes a request, the server creates an HttpSession object with a unique identifier and stores it in its map.

Are sessions shared between tabs?

By default all browsers share session state between multiple tabs. So if you logged into one tab with particular site and open internal link of the same site in new tab, you need not to worry to login again. It will automatically declare you as logged in user. For example, if I open google and login to google.com.

How does HTTP session work?

The client establishes a TCP connection (or the appropriate connection if the transport layer is not TCP). The client sends its request, and waits for the answer. The server processes the request, sending back its answer, providing a status code and appropriate data.


1 Answers

How would the server know when the browser is closed or the tab closed? At that point the browser doesn't send anything to the server.

This is a fundamental part of HTTP - it's a request/response protocol, not a "permanently open conversation" where you can tell if one party leaves the conversation. Think of it as a series of telegrams rather than a phone call - and you can't tell when you've received the last telegram you're going to get.

You'll need to design your way round this - to avoid needing to know when the browser has been closed. There are some ugly hacks to work around it - making AJAX poll the server with a heartbeat message, for example - but changing the design is a better solution.

like image 172
Jon Skeet Avatar answered Sep 21 '22 01:09

Jon Skeet