9.1 Cookie Overview
A cookie is a small, plain text message that the server sends to the browser, and the browser sends it to the server as it is when the user accesses the same Web server. By letting the server read the information it originally saved to the client, the site can provide a range of convenience for visitors, such as the identification of users in the online transaction process, the security requirements of the occasion to avoid users repeatedly enter the name and password, the homepage of the portal customization, targeted ads, and so on.
The purpose of cookies is to bring convenience to users and add value to the site. Although there are many misinformation, cookies do not pose a serious security threat. Cookies are never executed in any way, and therefore do not bring viruses or attack your system. In addition, because browsers generally allow only 300 cookies, each site holds up to 20 cookies, and each cookie has a size limit of 4 KB, so cookies won't fill your hard drive and will not be used as a denial of service attack.
Cookie API for 9.2 servlet
To send cookies to the client, the servlet first invokes the new cookie (Name,value) Create one or more cookies (section 2.1) with the appropriate name and value, set various properties (2.2) by cookie.setxxx, and add the cookie to the answer header (2.3) by means of a response.addcookie (cookie).
To read from the client cookie,servlet should call Request.getcookies (), and the GetCookies () method returns an array of Cookie objects. In most cases, you just need to iterate through each element of the array for a cookie with the specified name, and then call the GetValue method on the cookie to get the value associated with the specified name, which is discussed in section 2.4.
9.2.1 Create cookies
You can create cookies by calling the constructor of a cookie object. The constructor for the cookie object has two string parameters: the cookie name and the cookie value. Neither the name nor the value can contain whitespace characters and the following characters:
[ ] ( ) = , " / ? @ : ;