Cookie is a technology that maintains HTTP status information on the client. It is like a discount card issued by a mall. When a customer closes a shopping bill in a mall, the mall can decide whether to give the customer a discount card. The information recorded on the discount cards of different customers can be different. For example, the total amount and validity period of the customer's purchase. The customer can decide whether to accept this discount card. Once the customer accepts this discount card, he will carry this discount card every time he visits the mall, the mall will also handle some special transactions based on the information recorded on this discount card, such as calculating the discount rate and accumulating the current shopping amount. A cookie is a piece of data that is sent to the browser by the web server in the HTTP Response Header when the browser accesses a resource of the Web server, the data transmitted by the Web server to various client browsers can be different. The browser can decide whether to save the data. Once the web browser saves the data, it will, this piece of data should be returned to the web server in the HTTP request header. Obviously, the cookie is first sent by the Web server, and whether or not to send the cookie and the specific content of the sent cookie are completely determined by the Web server.
The Web server sends the cookie information to the browser by adding the set-Cookie response header field to the HTTP Response Message, the browser adds the cookie request header field to the HTTP request message to send the cookie back to the web server. A cookie can only identify one type of information. It contains at least one name and value ). A web site can send multiple cookies to a Web browser, so that multiple cookies can be used between the web browser and the Web server to transmit a variety of information, for example, A cookie is used to identify the visitor's name, and another cookie is used to identify the number of times the user logs on to the site. In addition to the name and set value, a cookie can have other additional attributes, such as the effective time of the cookie. If the cookie validity period is set, the browser process that accepts the cookie saves the cookie on the hard disk of the computer. The cookie is deleted only when the cookie expires, such cookies will be shared by multiple browser processes started on the same computer. Just as a customer can have discount cards provided by multiple malls, a Web browser can store cookies provided by multiple web sites. To prevent the cookie from filling the client's hard disk, the browser generally only allows 300 cookies to be stored, and each site can store up to 20 cookies. The size of each cookie is limited to 4 kb. If the cookie validity period is not set, the browser process that accepts the cookie only saves the cookie in its own memory space. When the browser process is disabled, all the cookies saved in it will also disappear.
Cookie enables a stateful session between the browser and the server. It connects all the programs on the same server accessed by a browser, transfer Data between these programs. For example, after a user logs on to a website using a browser, no matter which program the browser accesses, other programs can know the identity of the visitor, this is a very common application in web sites. This type of application is usually implemented using the cookie technology. After the Web server program verifies the user name and password in the login request, a user ID is generated, in the response message, the ID number is passed to the browser in the form of a cookie. Each time the browser accesses the web server, in the request message header, the IDs are automatically returned to the web server in the form of cookies, other programs on the Web server can identify the user who sent the current request. However, it should be noted that whether the cookie information not stored in the hard disk can be shared by multiple browser processes started on the same computer. Different browsers have different processing methods. For ie browsers, Cookies stored in the memory space of one browser process cannot be shared by other browser processes, in this case, Each browser process on the same computer forms an independent session with the server.
In Firefox, all processes and tabs share cookie information. In addition, press Ctrl-N in IE (or click "file" à "new" à "window" menu) to open a window or use a javascript window. windows opened by open statements share the cookie information of the original window because they belong to multiple windows within the same browser process.
Summary:
1. Cookie is a technology that maintains HTTP status information on the client. It is like a discount card issued by a mall.
2. A cookie is a piece of data that is sent to the browser by the web server in the HTTP Response Header when the browser accesses a resource of the Web server, the data transmitted by the Web server to various client browsers can be different.
3. Once the web browser saves a cookie, it should return the cookie to the web server in the HTTP Request Header each time it accesses the web server.
4. the Web server sends the cookie information to the browser by adding the set-Cookie response header field to the HTTP Response Message, the browser adds the cookie request header field to the HTTP request message to send the cookie back to the web server.
A cookie can only identify one type of information. It contains at least one name and value ).
5. A web site can send multiple cookies to a web browser. A Web browser can also store the cookies provided by multiple web sites.
Generally, a browser can only store 300 cookies. Each site can store up to 20 cookies. The size of each cookie is limited to 4 kb.
Cookie request header field:
The browser uses the cookie request header field to send the cookie information back to the web server.
Multiple cookies are sent back to the Web server through a cookie request header field.
The browser determines whether to send a cookie based on the following rules:
Whether the requested host name matches the domain attribute of a stored cookie;
Whether the requested port number is in the port attribute list of the cookie;
Whether the requested resource path is in the directory and subdirectory specified by the path attribute of the cookie;
Whether the cookie has expired.
Each cookie in the cookie request header field is separated by commas (,) or semicolons.
In addition to setting "name = value", the cookie request header field can have attributes such as version, path, domain, and port.
Add a "$" character as the prefix before the attribute names such as version, path, domain, and port.
The version attribute can only appear once and must be at the beginning of the cookie request header field setting value. If you need to set the path, domain, port, and other attributes of a cookie information, they must be placed after the "name = value" setting of the cookie information.
Comprehensive cookie instance --->
Cookie ckname = new cookie ("name", name );
Cookie cknickname = new cookie ("nickname", nickname );
Cknickname. setmaxage (365*24*3600 );
Cookie ckemail = new cookie ("email", "test1@it315.org ");
Cookie ckphone =
New cookie ("phone", "1111111 ");
Response. addcookie (ckname );
Response. addcookie (cknickname );
Response. addcookie (ckemail );
Response. addcookie (ckphone)
String lastnickname = NULL;
Cookie [] CKS = request. getcookies ();
For (INT I = 0; CKS! = NULL & I <cks. length; I ++)
{
If ("nickname". Equals (CKS [I]. getname ()))
{
Lastnickname = CKS [I]. getvalue ();
Break;
}
}
If (lastnickname! = NULL)
{
Out. println ("Welcome," + lastnickname );
}