Javaweb and Cookies

Source: Internet
Author: User

Cookies
1. HTTP protocol and Cookie (understanding)
* Cookies are made by the HTTP protocol! Cookies are saved to the browser by the server, and then the next time the browser requests the server, the last request is returned to the server.
* A key-value pair created by the server to be saved to the client browser! The server saves the cookie's response header:set-cookie:aaa=aaa set-cookie:bbb=bbb
>Response.AddHeader ("Set-cookie", "aaa=aaa"), Response.AddHeader ("Set-cookie", "bbb=bbb");
* When the browser requests the server, the cookie saved by the server is sent to the server with the request. The browser returns the cookie's request header:cookie:aaa=aaa; bbb=bbb
* HTTP protocol provisions (guaranteed not to give the browser too much pressure):
> 1 Cookies Max 4KB
> 1 servers Save up to 20 cookies to 1 browsers
Up to 300 cookies can be saved in a > 1 browser
* Browser wars: Because the browser competition is very encouraging, so many browsers will violate HTTP rules within a certain range, but will not let a cookie be 4gb!

2. Use of Cookies
* The server uses cookies to track client status!
* Save cart (items in cart cannot be saved with request because it is a user sending multiple request information to the server)
* Show last Login (also a user multiple requests)

**********cookie is not cross-browser! ***********

3. Use of cookies in Javaweb
* Original Way (Learn):
> Send Set-cookie response headers using response
> Get cookie request header using request
* Convenient Way (Proficient):
> UseRepsonse.addcookie ()method to save cookies to the browser
> Userequest.getcookies ()method to get the cookie returned by the browser

Cookie First example:
> A JSP to save cookies, a.jsp
> Another JSP gets the cookie! returned by the browser b.jsp

4. Cookie Explanation
* Cookies are not only name and value two properties
* MaxAge of cookies: The maximum life of a cookie, that is, the maximum amount of time a cookie can be saved. In seconds, for example: Cookie.setmaxage (60) indicates that the cookie will be saved to the hard drive by the browser for 60 seconds
> maxage>0: The browser will save the cookie to the client hard disk, and the valid length is determined by the value of MaxAge.
> Maxage<0:cookie only exists in the browser memory, and when the user closes the browser, the browser process ends and the cookie dies.
> maxage=0: The browser will immediately delete this cookie!
* Path (understanding) of cookies:
> The path of the cookie is not to set this cookie in the client's save path!!!
> Cookies are set when the path of the cookie is created by the server
> What cookies need to be returned to the server when the browser accesses a path on the server? This is determined by the path of the cookie.
> The path of the browser to the server, and if it contains the path to a cookie, the cookie will be returned.
> For example:
<> acookie.path=/day11_1/; bcookie.path=/day11_1/jsps/; ccookie.path=/day11_1/jsps/cookie/;
<> visit:/day11_1/index.jsp, return: Acookie
<> visit:/day11_1/jsps/a.jsp, return: Acookie, Bcookie
<> visit:/day11_1/jsps/cookie/b.jsp, return: Acookie, Bcookie, CCookie
> Cookie's path default: 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/
* Cookie Domain (Learn)
> domain to specify a cookie! Used when sharing cookies in multiple level two domains.
> For example, Www.baidu.com, zhidao.baidu.com, news.baidu.com, tieba.baidu.com sharing cookies can use domain
> Set Domain to: Cookie.setdomain (". baidu.com");
> Set Path to: Cookie.setpath ("/");


The cookie cannot exist in Chinese!!!

Save
Cookie C = new Cookie ("username", Urlencoder.encode ("Zhang San", "Utf-8"));//Error!
Response.addcookie (c);

Get
Cookie[] cs = request.getcookies ();
if (cs! = null) {
for (Cookie C:cs) {
if ("username". Equals (C.getname ())) {
String username = C.getvalue ();
Username = Urldecoder.decode (username, "utf-8");
}
}
}

Javaweb and Cookies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.