Session

session (session)
(1) What is a session?
A special object created by the server to maintain state.
(2) Working principle
When the browser accesses the server, the server will create a session object (the object has a unique id, called sessionId), the server will send the sessionId in the form of a cookie, and the server can use the sessionId to find the corresponding session icon.
(3) How to get session?
Method 1
HttpSession s = = request.getSession(true/false);
true:
first check if there is a sessionId in the request, if not, create a session object; if there is a sessionId, search for the corresponding session object according to the sessionId, if found If it is found, it returns, and if it is not found, a new session object is created.
false:
First check if there is a sessionId in the request, if not, return null; if there is a sessionId, look up the corresponding session object according to the sessionId, if found, return, if not found, return null.
Method 2
HttpSession s = request.getSession();
Equivalent to HttpSession s = request.getSession(true);

(4) Commonly used method
String getId() to obtain sessionId
setAttribute(String name, Object obj);
//This method may return null
Object getAttribute(String name);
//Unbind
removeAttribute(String name);
(5) Session timeout
a. What is session timeout
The server will delete session objects that have been idle for too long.
b. Default timeout time
The default time of the server is generally 30 minutes.
In the Tomcat folder, in ./conf/web.xml:
<session-config>
<session-timeout>30</session-timeout>
</session-config>
c.setMaxInActiveInterval(int seconds);
(6) How Immediately delete session
invalidate();

Filter
(1) What is a special component defined in the filter
servlet specification, which is used to intercept the calling process of the container.
Note:
After the container receives the request, it usually calls the service method of the servlet to process the request. If there is a filter, the container calls the filter method first.
(2) How to write a filter
step1, write a java class to implement the Filter interface.
Step2, in the doFilter method, write the interception processing logic.
step3, configuration (web.xml).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325211579&siteId=291194637