Use and management of HTTP sessions

Source: Internet
Author: User

Use and management of HTTP sessions

I. Introduction of the SESSION

HTTP is a stateless protocol that requires adding some additional data in the HTTP request to keep track of the customer's state to differentiate between different users.

A session mechanism is a universal solution for tracking customer status. A session is a series of interrelated interactions between a unit customer and a Web application over a period of time.

On the shopping web, a user starts shopping until the final checkout, the entire process is a session.

Second, javax.servlet.http.HttpSession interface

At the beginning of the session, the servlet creates a HttpSession object that holds the client state information. Each HttpSession object has a unique identifier. SessionID

Session Operation Flow

Three, the life cycle to participate in the conversation scope

Take a look at the three ranges:

Web Application scope: Shared data exists as a property of a ServletContext object

Request scope: Shared data exists as a property of a ServletRequest object

Session scope: The process of one session with a web app by the browser side, and the shared data as a property of the HttpSession object exists

Common methods for HttpSession interfaces

GetId (): Return SessionID

Invalidate (): destroys the current session, and the servlet container frees the resources occupied by the HttpSession object

SetAttribute (String name,object value): Saves a pair of name/value properties in the HttpSession object

GetAttribute (String name): Returns the value of the property stored in the HttpSession object according to the name parameter

Getattributenames (): Returns all the property names in the HttpSession as an array

Iv. Session Persistence

Save the HttpSession object in memory to the file system or database .

Benefits:

Save memory space---Transfer inactive httpsession objects to the file system or database, increasing the utilization of memory resources.

Make sure that the server resumes the session before restarting, or after a single web app restarts.

Servelt persists all properties that can be serialized, ensuring that shared data that is stored in session scope is not lost. ----Serializable property refers to the class to which the property belongs implements the Java.io.Seralizable interface.

Use and management of HTTP sessions

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.