Will the session be destroyed when the browser is closed?

Source: Internet
Author: User
Tags http request
topic:When the browser is closed, the session is destroyed. Answer:The unique identifier Jsessionid (SessionID) that exists on the browser disappears, but the sessionid stored in the server is not destroyed immediately.
Analysis:

We know that the session is a JSP nine built-in objects (also known as hidden objects) in one, its role is to save the current user's state information, learning it, think of the session's life cycle from opening a browser window to send requests to close the browser window, But in fact, this is not the correct statement. When a session starts, the servlet container creates a HttpSession object, and in the HttpSession object, it can hold the user state information, The servlet container assigns a unique identifier to the HttpSession object, that is, the Sessionid,servlet container stores SessionID as a cookie in the client's browser each time the user makes an HTTP request, The servlet container extracts the SessionID from the HttpServletRequest object and then finds the corresponding HttpSession object based on the SessionID to obtain the user's state information.

In fact, let the session end the life cycle, there are the following two ways:

One is the Session.invalidate () method, but this method is not recommended in the actual development, it may be used when the user is forced to log off;

One is that the session will expire after the current user and server interaction time exceeds the default time.

We know that the session is present on the server side, when the browser is closed, the browser does not send any request to the server to close the session, the natural session will not be destroyed, but can do a little effort, In all the client pages using JS Window.onclose to monitor the browser's closing action, and then send a request to the server to close the session, but this practice in the actual development is not recommended, the most normal way is not to control it, let it wait until the default time, automatically destroyed. So why do we not have access to the previous session when we close the browser? In fact, the previous session has been on the server side, and when we close the browser, the cookie is present in the browser process, when the browser is closed, the cookie does not exist.

There are actually two types of cookies:

One is the process that exists in the browser;

One is present on the hard disk.

While the session cookie is present in the process of the browser, then this cookie is called a session cookie, and when we reopen the browser window, the SessionID stored in the previous cookie no longer exists. At this time the server from the Tpservletrequest object does not check the SessionID, the server will send a new stored SessionID cookie to the client's browser, this time corresponding to a new session, The original session on the server will be destroyed automatically when it is the default time.


Additional:

When multiple tabs are opened simultaneously in the same browser, sending the same request or a different request is still the same session;

When the same browser is not opened in the same window, sending the request is still the same session;

When using a different browser, send a request, even if sending the same request, is a different session;

When the window of the current browser is closed, then opened, and the same request is launched, it is a different session.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.