Cookies are saved on the browser client
session is saved on the server side
Cookie API
Cookie class: Save session data
Create Cookie objects to save session data
new Cookie(java.lang.String name,java.lang.String value)
2) Setting Cookie Objects
void setPath(java.lang.String uri) Sets the cookie effective path
voidsetMaxAge(int expiry) Setting the effective time of cookie s
voidsetValue(java.lang.String newValue) Sets the cookie value
3) Send Cookie data to browser to save
response.addCookie(cookie);
4) Receive Cookie data sent by browsers
Cookie[] request.getCookies()
Cookie's Principles
1) Create Cookie data on the server side, then send the cookie data to the browser for storage through the set-cookie response header
Response header: for example: set-cookie: name=eric
2) The browser gets the cookie data sent by the server and saves it in the browser directory.
3) The next time the browser accesses the server, it will bring cookie data to the server. Send cookie data to the server through the cookie request header
Request header: e.g. cookie: name=eric
4) Server can accept cookie data sent by browser
request.getCookies();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. Create cookie objects and save session data //If you want to send Chinese, you must use URLEncoder to encrypt it first. String n = URLEncoder.encode("Zhang San","utf-8"); Cookie c = new Cookie("name", "zdh"); Cookie c1 = new Cookie("email", "11mqzdh2013@163.com"); //Set a valid path, by default, to the root directory of the current project //c.setPath("/day11"); /*Setting the effective time of cookie s * Positive Integer: Represents the time when the value of a positive integer exceeds, and the cookie will be lost!! (Cookie saves the browser's cache Negative integer: Indicates that if the browser is closed, cookies will be lost! (cookie saves browser memory) (default) 0 : Represents deleting cookie s with the same name */ //c.setMaxAge(10);//10 seconds after cookie disappeared c.setMaxAge(-1); //Close the browser cookie and disappear. It's also the default. c1.setMaxAge(50); //2. Send cookie data to browser, and carry cookie data to browser (set-cookie) through response header. //response.setHeader("set-cookie", "name=zzz"); //Simplified version response.addCookie(c); response.addCookie(c1); //3. The browser carries cookie data on its next visit and sends it to the server (cookie) through the request header. //4. Browsers get cookie data sent by browsers. // String name = request.getHeader("cookie"); // System.out.println(name); Cookie[] cookie = request.getCookies(); if(cookie!=null){ for(Cookie ck:cookie){ String name = ck.getName(); String value = ck.getValue(); System.out.println(name+"="+value); } }else{ System.out.println("No, cookie Information!!"); } }
Attention needs to be paid to: \\\\\\
1) Set the cookie effective path: setPath(uri). If the cookie is in a valid path, the browser will bring cookie data to the server the next time it accesses the valid path.
2) Set the cookie's effective time: setMaxAge (integer)
Positive integer: cookie data is stored in the browser's cache directory (hard disk). For example, 10. The cookie will be lost in 10 seconds. (Starting with the last cookie visit)
Negative integer: cookie data is stored in the browser's memory, and when the browser closes the cookie data, it will be lost.
Zero: Delete cookie s with the same name
3) Multiple cookie data can be sent to the browser at the same time, which must be of string type. Browsers generally only allow 300 Cookies, up to 20 Cookies per site, and the size of each Cookie is limited to 4KB.