Cookie Introduction and JSP Processing Cookie
2003-10-18 21:19:08
1. What is cookies? Everyone knows that between the browser and the web server communicates between the HTTP protocol, when a user issues a page request, the web server is just a simple response, and then turn off the connection with the user. Therefore, when a request is sent to the web server, whether it is the first visit, the server will treat it for the first time, so that it is impossible to know. To make up for this defect, Netscape developed a valid tool for cookies to save a user's identification information, so people nicknamed "small cake." Cookies is a WEB server to store information on a visitor's hard disk: Netscape Navigator uses a cookie information received from all sites using a cookies.txt local file; and the IE browser saves the cookie information in similar Under the catalog of C: WindowsCookies. When the user accesses a site again, the server will ask the browser to find and return to previously sent cookie information to identify this user. Cookies have benefits to websites and users: 1, cookies can make the site track the number of visits to specific visits, last access time and visitors enter the site of the site 2, cookie can tell the online advertiser advertising Clicks, This can be more accurately putting advertisements 3. The cookie has not expired. Cookie enables users to enter some sites that have been viewed without typing passwords and usernames, and cookies can help sites to share personal data to achieve each Various personalized services In JSP, we can also use cookies to write some powerful applications. Below, I want to introduce how to create and process cookies with JSP. Two. How to create cookies has said so many, everyone must know how JSP is created cookies. JSP is to create a cookie: cookie cookie_name = new cookie ("parameter", "value"); for example: cookie new cookie = new cookie ("usrname", "Waynezheng"; response.addcookie (NewCookie) [Explanation: JSP is to create a cookie with the appropriate name and value of the Cookie object, then use the appropriate name and value, then cookie can join the set-cookie response header by httpservletResponse method, this example has a cookie object Two strings parameters: username, Waynezheng. Note that the names and values cannot contain blank characters and the following characters: @:;?, "/ [] () = Handling the cookie properties See here, some friends have to ask: I know how to create a cookie? Use it? Yes, light knows how to create a cookie without knowing how to use it. In JSP, the program is set by cookie.setxxx, read the cookie.getxxx reads the property of cookie, now the main cookie Properties, and their methods are listed for your reference:
Type method method explanation stringgetComment () Returns a comment in cookie, if not comment, return null value. String getDomain () Returns the domain name applicable to cookies in cookies. Use the getdomain () method to indicate the browser to return cookies to the same domain Other servers, and usually cookies only return to the server that is identical to the server name sent. Note that the domain name must be attempted to return to the maximum time before the cookie expires at a point (for example .y .y.com) Int getMaxage (). StringgetName () Returns the name of the cookie. The names and values are two parts we have always cared, and the author will introduce getName / setName later. StringgetPath () Returns the path to the cookie. If the path is not specified, the cookie will return to all the pages in the current page where the current page is located and its subdirectory. BooleangetSecure () If the browser sends cookies through the security protocol, returns the true value if the browser uses the standard protocol, returns a false value. String getValue () Returns the value of the cookie. The author will also introduce GetValue / SetValue later. IntgetVersion () Returns the version of the protocol complied with Cookies. VoidSetComment Sets the comments in the cookie. VoidSetDomain (String Pattern) Sets the domain name VoidsetMaxage (int Expiry) for cookies to set the cookie expiration time. VoidSetPath (String Uri) Specifies the path to the cookie. Void SetSecure (Boolean Flag) Indicates the security protocol used by the browser, such as HTTPS or SSL. VoidSetValue (String NewValue) Cookies created a new value. VoidsetVersion (INT V) Sets the version of the protocol complied with Cookies. Read the client's cookie Send a cookie before you send a cookie, first create a cookie, then send an HTTP header with the AddCookie method. JSP will call Request.getCookies () to read the cookie from the client, and the getCookies () method returns an array of Cookie objects corresponding to the contents of the HTTP request header. You only need to use a loop to access the array of each element, call the getName method to check the names of each cookie until you find the target cookie, then get the value associated with the specified name to the GetValue method.