Time: 2016-11-17-22:32
--cookie
The server saves something to the client.
I. HTTP protocol and Cookies
1. Cookies are made by the HTTP protocol
Cookies are saved to the browser by the server, and the last request is sent to the browser the next time the browser requests the server.
2. A key-value pair created by the server and saved to the client browser
Server Save Cookie Response header: Set-cookie:aaa=aaa set-cookie:bbb=bbb
3. When the browser requests the server, the cookie saved by the server is sent to the server with the request
4. The browser returns the cookie request header
COOKIE:AAA=AAA; bbb=bbb
Return cookies: The browser accesses the server while submitting cookies.
5, HTTP protocol provisions (in order not to give the browser too much pressure)
* One cookie Max 4KB
* One server saves up to 20 cookies to a single browser
* A browser can hold up to 300 cookies
Browser wars: Because the browser is very competitive, many browsers will violate the HTTP protocol within a certain range, but not too much.
Second, the use of cookies
1. The server uses cookies to track client status.
2. Save the shopping cart (the item in the cart cannot be saved with the request because it is a number of requests sent by a user to the server)
3, display the last login name (also a user multiple requests)
4. Cookies cannot be used across browsers.
Iii. use of cookies in Javaweb
1, the original way
* Send Set-cookie response headers using response.
* Get the cookie request header using request.
2. Convenient way
* Use the Response.addcookie () method to save cookies to the browser.
* Use the Request.getcookies () method to obtain the cookie returned by the browser, or null if there is no cookie.
Iv. The first example of a cookie
1, a JSP save Cookie (a.jsp)
<%
Cookie cookie1 = new Cookie ("AAA", "AAA"); Response.addcookie (COOKIE1); Cookie cookie2 = new Cookie ("BBB", "BBB"); Response.addcookie (COOKIE2); This method is actually set Cookie header (Set-cookie)%>
Response will return cookie information:
2. Another JSP gets the cookie returned by the browser (b.jsp)
<%
cookie[] cookies = request.getcookies (); Returns an array of cookies
if (cookies = null) {for (cookie c:cookies) {out.println (c. GetName () + "=" + c.getvalue () + "<br/>"); } }
%>
Request will bring the cookie information on the hard disk to the server:
V. Explanation of Cookies
1. Cookies are not only name and value two properties.
2. MaxAge of Cookies:
* Cookies can be saved in the browser memory or can be saved on the hard drive.
* If MaxAge is not set, it is saved in browser memory by default.
* Maximum life of a cookie, that is, the maximum amount of time a cookie can be saved, in seconds, for example: Cookie.setmaxage (60), indicating that the cookie will be saved to the hard disk for 60 seconds.
* Maxage>0: The browser will save the cookie to the client's hard disk, and the valid length is determined by the value of MaxAge.
* Maxage<0:cookie only exists in the browser memory, when the user closes the browser, the browser process ends, and the cookie disappears.
* Maxage=0: The browser will delete this cookie immediately. (Memory and hard disk are not saved)
3. Path of the cookie:
* The path of the cookie is not set to save the cookie on the client.
* The path of the cookie is set when the cookie is created by the server.
* When a browser accesses a path to a server, does it need to return those cookies to the server? This is determined by the path of the cookie.
* The path to the servlet where the cookie resides is the path to the file where the cookie resides.
* The browser accesses the path to the server and, if it contains a cookie path, returns the cookie.
For example:
acookie.path=/day11_1/; bcookie.path=/day11_1/jsps/; ccookie.path=/day11_1/jsps/cookie/;
1) Visit:/day11_1/index.jsp, return: Acookie
2) Visit:/day11_1/jsps/a.jsp, return: Acookie, Bcookie
3) Visit:/day11_1/jsps/cookie/b.jsp, return: Acookie, Bcookie, CCookie
Path default value for cookies:
The parent path of the current access path.
For example, when accessing/day11_1/jsps/a.jsp, the cookie's default path is day11_1/jsps/
4. Domain of Cookies
* Domain names used to specify cookies are used when sharing cookies in multiple level two domains.
* The virtual host needs to be configured.
* For example, you can use domain when sharing cookies between www.baidu.com, zhidao.baidu.com, news.baidu.com, tieba.baidu.com.
* Set Domain to: Cookie.setdomain (". baidu.com");
Cookies can be accessed as long as the. baidu.com is the suffix.
* Set Path to: Cookie.setpath ("/");
The cookie path must be a slash, the project name cannot be written, and once the project name is written, it is fixed for use by a project.
Javaweb's Cookie