当前位置:网站首页>Cookies and sessions
Cookies and sessions
2022-07-26 20:19:00 【SpongeBob's nest】
Cookie
Cookie In essence, it is a mechanism that the browser provides to the page to store data persistently .
Persistence means that data will not be lost due to program restart or host restart , In fact, the data is stored in the disk .
If the data is stored in memory , Program restart or host restart , The data is released . And if it is written in the disk file , Read the disk file directly next time .
By default, the browser cannot let pages access your computer files , If the web page contains malicious code , Delete all the learning materials stored on your computer , Then this will be a sad story .
But sometimes we have to store some data , Easy access to subsequent web pages , The most common is to log in and store user information . When we log on to a web page , Be able to jump to the page at will without logging in again . This is because the server returns a user's identity information for the browser after we log in , The browser will open up a separate space for this identity , Throw some important information here , Only give access to this space . Then the next jump page will bring this identity information , So you don't need to log in again . and Cookie It is one of the typical small black room spaces .
Sign in gitee adopt fiddler Grab the bag , So let's look at Cookie How did it come from

We can clearly see that the server's response to the login request returns header Of 3 individual set sentence .
This is actually some specific information returned by the server to the client after authentication , The client can rely on this information , To complete subsequent page visits .
Session
Although it can pass Cookie Save the information of each website , But the capacity is also limited , What if there is not enough space ?
In fact, there's no need to worry about this , because Cookie In fact, not many things are preserved , The main information is still stored in the server . The server manages many Session( conversation ), Each session stores key user information ( essential information + Record + Behavior ), Every Session There is one. SessionId( Session ID ), and Cookie This is actually what is stored SessionId, In this way, the client only needs to send one to the server SessionId, The server will be based on SessionId Return a session .
Actually, it's easy to understand , We often see ~ for example QQ,

qq This server stores many chat records between us and our friends , Every record of chatting with friends is a conversation , Every friend's qq Number is a conversation id. We can talk id Find session .
Servlet in Cookie and Session Application
servlet For in the Cookie and Session All have good support , So we can Servlet Provided in Api, For session management .
HttpServletRequest class
getSession Method , It can be used to get the session on the server , It can also be used to create sessions . Specific behavior , Depends on the parameters .
If the parameter is true: session does not exist , Then create a session ; There is , Then get .
If the parameter is false: session does not exist , Then return to null; Conversation exists , Then get .
Calling getSession Specific things to do in method :
① Create a session : First get the request Cookie Inside SessionId Field , And then judge this SessionId Whether it exists on the server , If it doesn't exist , Enter the create session logic . Will create a HttpSession object , And generate a sessionId.(sessionId It's a long number , Usually with Hexadecimal ).
Next , Just put sessionId As key, Put this HttpSession object , As value. Put this key value right , Save to Server memory One of the “ Hashtable ” In such a structure .( In fact, it is not necessarily a hash surface , Instead, it is used to be similar to key/value Storage structure of ) And then , The server will return a HTTP Respond to , hold sessionId adopt Set-Cookie Field Back to the browser . then , The browser can save this sessionId To cookie It's in .② Get session : Get the... In the request first cookie Inside sessionld Field determines this sessionld Whether there is..., on the current server ( That is, whether there is in the hash table ), If there is , Just query this directly HttpSession object , And return by return value .
HttpSession class
One HttpSession Object contains multiple key value pairs . We can go to HttpSession Save any information we need


Cookie class
Every Cookie The object contains two more properties :name, value( Or key value pairs )

HttpServletResponse class

The response can be based on addCookie This method , To add a Cookie Information ( Key value pair ) To In the response message
Add the key value here , Will act as HTTP In response Set-Cookie Fields to represent .
Another way : Put one cookie Key value pair convert to character string Format , adopt Set-Cookie Back and forth client ( browser ) here .
Code example : Implement user login
① Agree on the front and back-end interaction interface

② Write code





upgrade



End .
边栏推荐
- AI 技术,让复杂世界简单化 | TeaTalk·Online 应用实战系列第 2 期
- Excel-VBA 快速上手(十、提示框、可输入的弹出框)
- Use request header authentication to test API interfaces that need authorization
- Kingbases SQL language reference manual of Jincang database (15. SQL statement: create materialized view to create schema)
- 徽商期货开户安全吗?请问徽商期货开户应该注意什么呢?
- tf.GraphKeys
- Array operations add, delete, modify, and query
- 靠元宇宙和NFT,天下秀疯狂“割韭菜”?
- 20220725树状数组入门反思
- Silent desktop fan chip dltap703sd Jericho
猜你喜欢
随机推荐
Codeforces Round #810 (Div. 2)(A~C)
this指向,最简单的规则记住它
Introduction to component functions of blueprism process business object Chapter 3 of RPA
Vite configuration eslint specification code
C # convert PDF files into pictures
ES6的方法&类数组转成真正的数组&判断数组的方法
Excel-VBA 快速上手(十一、字符串常用操作)
有点酷,使用 .NET MAUI 探索太空
go+mysql+redis+vue3简单聊室,第5弹:使用消息队列和定时任务同步消息到mysql
[cache series] advantages, disadvantages and choices of several cache read-write schemes
numpy.zeros_like
第一次培训课完美成功(๑•ㅂ•)و*
静音台式风扇芯片-DLTAP703SD-杰力科创
C# 客户端程序调用外部程序的3种实现方法
2000 words to help you master anti shake and throttling
A little cool, explore space with.Net Maui
three.js 给地球加标签和弹窗
用 QuestPDF操作生成PDF更快更高效!
svn使用碎碎念
I hope some suggestions on SQL optimization can help you who are tortured by SQL like me








