Cookie
Conversation
The user opens a browser, clicks on many hyperlinks, accesses multiple web resources, and closes the browser. This process can be called a session;
- Stateful conversation: A classmate has been to the classroom, and next time we come to the classroom, we will know that this classmate has been there before, which is called a stateful conversation;
*A website, how do you prove that you have been there? *
How does the server prove that the client has come
-
The server sends a letter to the client, and the client can bring the letter next time it visits the server; cookie
-
The server registers that you have come, and I will match you next time you come; session
Two techniques for saving sessions
*cookie*
- Client technology (response, request)
*session*
- Server technology, using this technology, you can save the user's session information. You can put information or data in the Session!
Common scenario: After logging in to the website, you don’t need to log in again next time, you will go directly to the second visit
Cookie
-
Get cookie information from the request
-
Server responds to client cookie
Cookie[] cookies = req.getCookies(); //获得Cookie
cookie.getName(); //获得cookie中的key
cookie.getValue(); //获得cookie中的value
new Cookie("lastLoginTime",System.currentTimeMillis()+""); //新建一个cookie
cookie.setMaxAge(24*60*60); //设置cookie的有效期
resp.addCookie(cookie); //响应给客户端一个cookie
*cookie: generally saved in the local user directory appdata;*
Is there an upper limit for a website cookie?
-
A cookie can only store one information
-
A web site can send multiple cookies to the browser, up to 20 cookies can be stored
-
Cookie size limit
-
300 cookie browser limit
*Delete Cookies*
-
If you don’t set the validity period, close the browser and it will automatically expire;
-
Set the validity period to 0;
*encode decode*
URLEncoder.encode("家明","utf-8")
URLDecoder.decode(cookie.getValue(),"UTF-8")