HttpSession explanation,
1: session authentication principle:
When the client accesses the server for the first time, the client request does not carry any identifier to the server, so the server cannot find the corresponding
Session, so the session object will be created. When the server responds, the server will put the session identifier in the Set-Cookie of the Response Header
Return key-value to the client, for example, JSESSIONID = 7F149950097E7B5B41B390436497CD21; where JSESSIONID is fixed,
The value next corresponds to the ID of the newly created session for the client. When the browser accesses the server again, the client will
Put it in the cookie and request the server, the server will find the corresponding session object based on this ID; (when the browser is closed, the session ends, because the cookie is cleared
The corresponding session Object Identifier disappears, and the corresponding session still exists, but it has become decommissioned data waiting for GC to be recycled)
You can use this method to obtain the corresponding session ID: session. getId ();
2: session lifecycle:
For example, you can use the following method:
3. Examples of common methods:
Logon interface:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4
Display interface:
1 <% @ page language = "java" contentType = "text/html; charset = UTF-8" 2 pageEncoding = "UTF-8" %> 3 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 4
Logout interface:
1 <% @ page language = "java" contentType = "text/html; charset = UTF-8" 2 pageEncoding = "UTF-8" %> 3 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 4
When you log on to the console for the first time:
Enter the name Lily in userName and click "Log on:
When you click "re-login" again, the session is used for data echo. The session is not canceled, so the session remains unchanged:
When the Logout button is clicked, the logon session becomes invalid and the original session object is destroyed:
After you click "Log on again" or refresh the page, a new session will be created: