Java web ---- Cookie
1. We know that HTTP is a stateless protocol, that is, each request is independent! The status of the previous request cannot be recorded. However, cookies can be used in HTTP to track sessions!
In Java Web, sessions are used to track sessions. The underlying layer of sessions depends on Cookie technology.
2 Cookie
A Cookie is a key-Value Pair created by the server and sent to the client through a response. The client saves the Cookie and marks the Cookie source (the server's Cookie ). When the client sends a request to the server, it will include all the server cookies in the request and send them to the server so that the server can recognize the client!
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + MyBjb29raWXA/dfTMTwvcD4KPHA + PC9wPgo8cD6/large + large/AwMb3u + large + cjxpbwcgc3jjjpq = "http://www.2cto.com/uploadfile/Collfiles/20150108/20150108093155383.png" alt = "\">
Package com. cug. cookie; import java. io. IOException; import java. util. UUID; import javax. servlet. servletException; import javax. servlet. http. cookie; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class AServlet extends HttpServlet {@ Overrideprotected void doGet (HttpServletRequest req, HttpServletResponse resp) throws S ErvletException, IOException {resp. setContentType ("text/html; charset = UTF-8"); String id = UUID. randomUUID (). toString (); Cookie ck1 = new Cookie ("id", id); resp. addCookie (ck1); resp. getWriter (). print ("cookie already set! ");}}
package com.cug.cookie;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class BServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setContentType("text/html;charset=utf-8");Cookie[] cookies = req.getCookies();for(Cookie c: cookies){if(c.getName().equals("id")){resp.getWriter().print(c.getValue());}}}}
Case 4: display the last access time
Package com. cug. cookie; import java. io. IOException; import java. util. date; import javax. servlet. servletException; import javax. servlet. http. cookie; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class CServlet extends HttpServlet {@ Overrideprotected void doGet (HttpServletRequest req, HttpServletResponse resp) throws S ErvletException, IOException {resp. setContentType ("text/html; charset = UTF-8"); Cookie ck = new Cookie ("lasttime", (new Date ()). toString (); resp. addCookie (ck); String s = "this is your first visit to this site! "; Cookie [] cookies = req. getCookies (); if (cookies! = Null) {for (Cookie c: cookies) {if (c. getName (). equals ("lasttime") {s = "your last access time is:" + c. getValue () ;}} resp. getWriter (). print (s );}}
5 Chinese Cookie names need to be encoded
Package com. cug. cookie; import java. io. IOException; import java.net. URLDecoder; import java.net. URLEncoder; import javax. servlet. servletException; import javax. servlet. http. cookie; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class DServlet extends HttpServlet {@ Overrideprotected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp. setContentType ("text/html; charset = UTF-8"); String name = URLEncoder. encode ("zhu", "UTF-8"); String value = URLEncoder. encode ("zhu", "UTF-8"); Cookie ck = new Cookie (name, value); resp. addCookie (ck); Cookie [] cookies = req. getCookies (); for (Cookie c: cookies) {if (c. getName (). equals (name) {String name1 = URLDecoder. decode (c. getName (), "UTF-8"); String value1 = URLDecoder. decode (c. getValue (), "UTF-8"); resp. getWriter (). print (name1 + ":" + value1 );}}}}