Session: when a user opens a browser and accesses a website, the server allocates a space for the browser in the memory of the server, and the modification space is exclusive to the browser. This
Session: when a user opens a browser and accesses a website, the server allocates a space for the browser in the memory of the server, and the modification space is exclusive to the browser. This space is the session space. The default data storage time in this control is 30 minutes, which can be modified. Session. setMaxInactiveInterval (n seconds );
Session usage:
1. Shopping Cart in online shopping mall
2. save login user information
3. Put some data into the session for each page of the same user 4. Prevent Unauthorized user logon to a page
How to Understand Session:
The session can be viewed as a table with two columns. Each row is a property of the session. Each attribute contains two parts: the name of the attribute (String) and the value of the attribute (Object ).
How to Use Session:
1. Get the session
In servlet: HttpSession hs = request. getSession (true );
In JSP, session is a built-in object that can be directly used,
2. Add attributes to the session
Servlet: hs. setAttribute (String name, Object o)
Session. setAttribute (String name, Object o) in JSP)
3. Obtain an attribute from the session
In servlet: hs. getAttribute (String name );
In JSP: session. getAttribute (String name );
4. delete an attribute from the session
In servlet: hs. removeAttribute (String name );
In JSP: session. removeAttribute (String name );
Session considerations
1. The default time for Attribute existence in a session is 30 min. You can also modify the time for Attribute existence: a. modify web. xml B in tomacat and modify it in the program.
2. The above mentioned 30 Minutes indicates the user's daze time, rather than the accumulated time.
3. When a browser accesses a website, the server will fly and the browser will assign a unique session id to distinguish different browsers (clients)
4. Because each session attribute needs to occupy the server's memory, software companies are forced to use it. A. Save User Information B. Save the shopping cart