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