Comparison of cookies and session
1. For cookies:
① Cookies are created on the server side
② Save cookies on the browser
③ The life cycle of a cookie can be set through cookie.setMaxAge(2000); if setMaxAge is not set,
Then the life cycle of a cookie disappears when the browser closes.
④Cookies can be shared by multiple browsers of the same type. Can imagine cookies as a table
Compare:
① Where it exists:
Cookies exist in the client, temporary folder
session: exists in the server's memory, and a session domain object serves a user's browser
②Safety
Cookies are stored in plain text on the client, with low security and can be encrypted and stored through an encryption algorithm
Session is stored in the server's memory, so it is safer
③Network transmission volume
Cookies will pass messages to the server
The session itself is stored on the server and there will be no traffic to be transmitted.
④ Life cycle (taking 20 minutes as an example)
(1) The life cycle of a cookie is accumulated, and it starts to be timed from the time it is created. After 20 minutes, the life cycle of a cookie ends.
(2) The life cycle of the session is interval. From the time of creation, the time starts. If the session is not accessed within 20 minutes, the session life cycle will be destroyed.
However, if the session has been accessed within 20 minutes (such as at the 19th minute), the life cycle of the session will be recalculated
(3) The opportunity to close the session life cycle, but it has no effect on cookies
⑤Access Scope
session is exclusive to a user's browser
Cookies are shared by multiple users' browsers
Methods to invalidate session:
1. Close tomcat
2.reload web application
3.session time is up
4 invalidate session
The above article is based on the comparison of cookies and sessions in Java. This is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.