当前位置:网站首页>The difference between cookies and sessions

The difference between cookies and sessions

2022-07-06 13:40:00 Wake up duck, did you program today?

Cookie: It is a mechanism used by the client browser to save the data of the server , When accessing a web page through a browser, the server can send some state data as key-value In the form of Cookie Store in the client browser , Then the next time the client accesses the server, it will carry the data and send it to the server , The server can Cookie The contents carried in it can be identified

Session: A container object indicating that the session belongs to the server side. By default, it will request for each browser ,Servlet Each container will be assigned a Session object ,Session The essence of can be considered as a ConcurrentHashMap It can be used to store some state data generated by the current session ,Http Itself is a stateless agreement , In other words, the server does not know that the multiple requests sent by the client belong to the same user , therefore Session To make up for Http A deficiency of Statelessness .

Simply put, the server side can take advantage of Session To store a record of multiple requests generated by the client in the same session, then based on the server Session The storage mechanism of is combined with that of the client Cokkie Mechanism, we can implement a stateful Http agreement

working principle :

When the client accesses the server for the first time , The server side will create a session for this request and generate a unique SessionID To mark this session , Then the server puts this SessionID Write to the client browser Cookie Inside , It is used to save the client state , It will be carried every time in the subsequent request SessionID, The server can use this SessionID To identify the status of the current session .

On the whole Cookie It is the storage mechanism of the client , and Session It is the storage mechanism of the server

 

原网站

版权声明
本文为[Wake up duck, did you program today?]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060916451958.html