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

后端 未结 8 1720
-上瘾入骨i
-上瘾入骨i 2020-12-06 00:54

I have the following implementation of HttpSessionlistener

public class SessionListener implements HttpSessionAttributeListener, HttpSessionListener {


publ         


        
8条回答
  •  时光说笑
    2020-12-06 01:15

    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.

提交回复
热议问题