Will the session be destroyed after the Java Web browser is closed?

Source: Internet
Author: User
Tags session id java web

Will the session be destroyed after the browser is closed?

The session is one of the nine built-in objects (also known as hidden objects) of the JSP that holds the current user's state information, and the beginner may think that the session's life cycle is from opening a browser to sending a request to closing the browser, which is wrong.

Running mechanism of Session

The first time a user accesses a web app that supports a session, a new session is opened. When a user browses to a different page of the Web app, it is always in that session. In specific terms:

1 when a new session is opened, the servlet container creates a HttpSession object that can hold information about the user state.

2 The servlet container assigns each HttpSession object a unique identifier, the session ID, which is stored as a cookie in the client-side browser.

3 each time the user sends an HTTP request, the Servlet container takes the session ID from the HttpServletRequest object and then finds the corresponding HttpSession object based on the session ID to obtain the user's state information.

Two ways to end the session life cycle:

1 session.invalidate ()

Used to force logoff of users.

2 Automatic invalidation

The session is invalidated when the user interacts with the server longer than the default time.

After you close the browser, the browser does not send a request to the server to shut down Session,session will not be destroyed. Although it is possible to use JS's window.onclose on all client pages to listen for the browser's close action, and then send a request to the server to close the session, it is not recommended. The most normal way to handle this is to automatically destroy it when it times out. Cookies that exist in the browser after you close the browser do not exist. There are two types of cookies, the browser's process and the hard disk, respectively. While the session cookie exists in the process of the browser, when re-opening the browser, the previous cookie stored in the session ID no longer exists, the server from the HttpServletRequest object does not find the session ID, The server will then send a new cookie with session ID to the browser, which opens a new session, and the server will automatically be destroyed when it expires. Among them, different browsers open different session sessions.

  

Resources

Does the session end after the browser is closed?

Will the session be destroyed after the Java Web browser is closed?

Related Article

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.