Java Web Learning summary Article 6-understanding the Session mechanism and Java Web Article 6
Understanding Session mechanism
As mentioned above, common Session tracking has two technologies: Cookie and Session. Today, I will explain the Session mechanism for you.
What is Session?
Session is another mechanism for recording the customer status. The difference is that the Cookie is stored in the client browser, and the Session is saved on the server. When the client browser accesses the server, the server records the client information on the server in some form, which is Session. When the client browser accesses the client again, you only need to find the customer's status from the Session.
Achieve User Login
The class corresponding to the Session is javax. servlet. http. HttpSession class. Each visitor corresponds to a Session object, and all the status information of this customer is stored in this Session object. The Session object is created when the client first requests the server. The Session is also a key-value Attribute pair. The getAttribute (String key) and setAttribute (String key, Object value) methods are used to read and write the customer status information.
Session Lifecycle
The Session is stored on the server side. To achieve higher access speed, the server generally stores the Session in the memory. You must note that a Session is created only when you access jsp, Servlet, and other programs. sessions are not created when you access static resources such as HTML and IMAGE. After a Session is generated, the server updates the last access time of the Session and maintains the Session.
As more and more users access the server, the number of sessions increases. To prevent memory overflow, the server deletes sessions of idle users from the memory for a long time. This is the Session Timeout time. If the server is not accessed after the timeout, the Session will automatically fail.
The timeout value of the Session is the maxInactiveInterval attribute. You can obtain the timeout value through getMaxInactiveInterval () and modify it through setMaxInactiveInterval (long interval.
Common Methods of Session
URL address Rewriting
Although the Session is stored on the server and transparent to the client, the normal operation of the Session also requires support from the client browser. This is because the Session needs to use cookies as the identification mark. The HTTP protocol is stateless, and the Session cannot determine whether it is the same user based on the HTTP connection. Therefore, the server sends a Cookie named JSESSIONID to the client browser, the value is the Session ID (HttpSession. return Value of getId ). The Session identifies whether the Session is the same user based on the Cookie.
What if the client browser disables or does not support cookies? For example, many mobile phones do not support cookies. Java Web provides a solution: URL address rewriting.
The URL address rewriting principle is to overwrite the Session id information of the user to the URL address. The server can parse the rewritten URL to obtain the Session id. In this way, you can use Session to record user status even if the client does not support cookies. The HttpServletResponse class provides encodeURL (String url) for URL address rewriting.