The reason why the session has not expired and its saved data is lost without reason

Question:

A function we often do is to save the login user information to the session and display the login user name on the page. However, if the page is not refreshed in a short time or even a few seconds, the user name disappears. In fact, the user data saved in the session is lost.

Baidu has done a lot. Most of them say that the session expiration time is set a little longer, or the configuration problem of tomcat, etc., but they have not been solved When I asked this question in my last blog, a blogger's comment gave me ideas. Sure enough, O (∩ ∩) O ha ha ~ thank you first!!!

Then look at the test process ↓↓

The following is the user entity class:

User servlet: address bar input http://localhost:8080/session/UserServlet Access, and then redirect to showuser JSP (so that the servlet will not be accessed when the page is refreshed)

Session listener: monitors the creation and destruction of sessions, adds attributes to sessions, removes attributes, and changes attributes.

showUser. JSP displays the user name

The preparations are ready. Let's look at the test:

First, directly enter the address in the address bar to access the userservlet. In doget(), the user information will be saved to the session and redirected to showuser JSP, the user name and information are displayed, as shown in the figure: you can see that the properties and strings of the user object can be displayed.

The following is the information output from the console: the screenshot just after accessing the servlet

Then, after a while, the console is shown as follows:

You can see that after 54 seconds, the user attribute is removed, the message is not removed, and the session is not destroyed!!! Then refresh the page display: This is what we often see

Then, baidu mostly said that setting the session expiration time is a little longer: but it has no effect

Then there is another method: put a refresh. Net file in the root directory JSP file, the contents are as follows: it is mainly the sixth line of code, which is refreshed every second and linked to itself

Then add a code on the page displaying data (showuser. JSP): < iframe width = 0 height = 0 SRC = "refresh. JSP" > < / iframe >

The purpose is to constantly request the server. My understanding is that the server will not remove this data if it thinks that the client has been using this object

Although this has achieved the goal, it has great disadvantages, and the symptoms are not the root cause: it is certain that the server has been requested to consume server resources, and the browser's refresh icon will jump all the time. It looks very uncomfortable

*****************************************************************************************************************

Finally, let's look at the solution: just implement the user serializable interface, as follows:

The object to be saved to the session implements the serializable interface, so that the user data will always be saved in the session. No matter how you refresh, the data will always be saved unless the session is destroyed!!!

So what is the reason? Here is a paragraph from Baidu:

[object serialization allows you to convert objects that implement the serializable interface into byte sequences. These byte sequences can be completely stored for later regeneration of the original object. For example, when transmitting over the network, you can transmit either a string or an object, such as a socket. When transmitting a string, the receiver can easily parse it, but transmit a string When reading an object, the other party doesn't know what object you passed after reading the byte stream, so it can't convert it into the original object and parse the object's properties. At this time, serialization and deserialization are used. Class implements the serialization interface. Socket sends the byte stream of this object. After receiving the stream, the receiver can deserialize the stream into an object, and then use the methods and properties of the object.]

Attach test code: session data loss test code

OK!!!

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
分享
二维码
< <上一篇
下一篇>>