How to clear the httpsession, if the user close the browser in java

前端 未结 5 1704
闹比i
闹比i 2021-01-03 01:51

I am trying to clear the HttpSession if the consumer close the browser window. I dont know how to do it, Please help me anyone

Thanks & Regards Chakri

相关标签:
5条回答
  • 2021-01-03 02:35

    if you close the browser it is already clear the session

    but if you want to force to clear the session

    usesession.invalidate(); or if you want to remove specific session use session.removeAttribute("<sessionName>");

    0 讨论(0)
  • 2021-01-03 02:37

    EDITED :

    try reading the answer on this Page

    "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."

    SUMMARIZATION :

    YOU CAN'T DO IT

    Possible Workaround :

    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.

    0 讨论(0)
  • 2021-01-03 02:42

    Short answer: you can't. Fortunately, sessions expire automatically after a period of time; check your servlet engine documentation to see how to set this timeout period.

    I felt that there should be a way to do it from JavaScript; unfortunately, JavaScript doesn't seem to have an onWindowClose handler.

    0 讨论(0)
  • 2021-01-03 02:50

    It can be archived in a little diffrent way.

    1. use a javascript event known as "window.onbeforeunload"

      e.g window.onbeforeunload=mylogic_function(); function mylogic_function() { window.location.href = "path to the servlet which inavalidate the session"; }

    2. Have a dedicated servlet which job is only to deactivate the session

    Try this ..hope this will work

    0 讨论(0)
  • 2021-01-03 02:51

    If you could get the browser to (reliably) notify the server that the user had closed the window, then the server could call session.invalidate() as per the original Answer provided by ejay_francisco.

    The difficulty is getting the notification to happen reliably. There are various ways to detect the window close; e.g. as covered by these Questions (and similar):

    • Trying to detect browser close event
    • javascript to check when the browser window is close
    • javascript detect browser close tab/close browser

    You could then write the (javascript) close event handler to send a specific request to the server.

    However, I don't think any scheme is going to be able to deal with cases where the user's browser dies, the user's machine is shut down, and similar scenarios. So if you need the session to be cleared 100% of the time, then you are probably out of luck. I don't think it can be done.

    0 讨论(0)
提交回复
热议问题