Understand session in Java

Let's take a look at a better description of session:

A session is a session that can save user information without closing the browser. It is like a temporary container to store these temporary things. For example, if the logged in saved user information jumps from one web page to another, the user information can be saved in session, and the website shopping cart can be realized in session

Why session

This is to fill the limitation of HTTP protocol. When a user visits a page, the server returns the request (for example, after you visit a web page, the page will present the page content and interface UI to you). Even if it is over, it will be disconnected. The server will no longer track the task status of the client (browser). Therefore, each HTTP request is independent and discontinuous, HTTP is also called stateless protocol. If we want to operate some user's own data on an occasion or a specific process, it will be very troublesome and even dangerous. For example, you can interact with the server by passing parameters through the URL and realize the operation;

The emergence of session solves this problem. Session is created on the server and destroyed by the server after a certain time. During this period, the session between the client and the server will be maintained, and the client will use the session information on the server to find or operate some data;

How to use session

Java API only gives us a way to get the session related to the current session:

Set value:

Get value:

Delete the session specified attribute key:

Clear all sessions to completely invalidate the current session:

Session timeout period setting

1. Tomcat installation location: conf / Web xml :

30 minutes

2. Tomcat installation location: conf / server xml :

In seconds

3. Java code settings:

0 minutes:

Other instructions:

1. Session Expiration:

1>. Client browser close:

2>. Session session expires;

3>. The client session called invalidate();

2. Whether the browser is closed and the session is still active;

After the client browser is closed, the session will still exist on the server for a certain time. Only when the browser is opened again, a new session will be generated. The browser matches the session of the server through the generated sessionid attribute; Although the last session is still there, you can't access it;

3. <% @ page session = "false"% > what is the situation

This sentence means that the session cannot be used at present, but the page session can still be created;

4. When is the session created

Call HttpServletRequest in the program Created when getsession (true); If the page does not use <% @ page session = "false"% >, httpsession = HttpServletRequest will be automatically added when the JSP page is compiled into a servlet getSession(true);

summary

The above is all about understanding the session in Java. I hope it will be helpful to you. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>