First, preface As early as HTML just appeared, there is no way to record and identify different users between each independent page. Later, people invented cookie technology. When the user accessed the web, it could create a file on the visitor's machine. We call it as cookie, write a content into it, to identify different users. If the next user accesses this web page, it can read the content inside this file so that the page will know that the last user has visited the webpage. Although the fabrication technology now has developed many years ago. But sometimes, cookies can still help us with a lot of busy. Next, let's take a look, how to operate cookies with JSP when writing JSP files. Second, write cookies actually use JSP operation cookies is very simple, let's look at the following JSP program: ..... (middle) <% string cookiename = "sender"; Cookie cookie = new cookie (cookiename, "test_content"); cookie.setMaxage (10); response.addcookie (cookie);%> ........ (Other content) This is set up a cookie Is it simple? Let's take a closer study: cookie cookie = new cookie (cookiename, "test_content"); this line establishes a cookie object, initialization has two parameters, the first parameter cookiename defines the name of the cookie The latter parameter is also a string that defines the content of the cookie. That is, we want the web page to identify files on the user's machine. Next line: cookie.setMaxage (10), calls the setMaxage method in cookies, setting the survival of cookies on the user's hard drive for 10 seconds. A cookie exists in the user's hard drive. When building a cookie object, we must formulate the survival of cookies. After this survival, the cookie file will no longer work, will be viewed by the user. Delete itself by yourself. If we want users to visit this page next time, the cookie file is still valid and can be read by the web page, we can set the Cookie's survival time. For example, cookie.setMaxage (365 * 24 * 60 * 60) allows the cookie file to be valid within one year. III. After reading the cookie cookie file, naturally, we naturally need to read it out, otherwise we are not a white fee? Next, let's see how to read the cookie on the user's hard drive.
........ (middle) Name value <% cookie cookies [] = request.getCookies (); cookie scookie = null; string svalue = null; string sname = null; for (int i = 0 i {scookie = cookies [i]; svalue = scookie.getValue (); sname = scookie.getname ();%> <%}%> name value <% = Name%> <% = svalue%> . ....... (Other Content) This small segment JSP file can read all the effective cookies on the user's hard drive, which is still a cookie file in the survival period. And list each cook as a form Names and content. We come to study this code: cookie cookies [] cookie cookies [] = Request.getCookies () We use request.getCookies () to read the cookies on the user's hard drive, and put all the cookies Go to a cookie object array. Next we use a loop statement to traverse a cookie object array, we use Scookie = cookies [i] to remove a cookie object in the array, then we use Scookie.getValue () and Scookie .getname () two methods to get this cookie's name and content. By putting the cookie's name and content in the string variable, we can do a variety of operations. Only examples In the traversal of the cycle statement, all cookies can be displayed in a table. Some issues that need to pay attention to the above two simple examples, can be seen, use JSP to perform cookie operations It is very simple. However, we have to pay attention to some problems in actual operations: 1. Cookie compatibility problem cookie's format has 2 different versions, the first version, we call Cookie Version 0 It is initially developed by Netscape, which is also supported by almost all browser. And the new version, cookie Version 1 is based on the RFC 2109 document. To ensure compatibility, Java regulations, mentioned above The operation involving cookie is done for older version of cookies. And the new version of cookies are not currently javax.servlet.http.cookie Supported by the package. 2. The content of the cookie's content of the same cookie is also different for different cookie versions. In cookie version 0, some special characters, such as spaces, square brackets, parentheses, equal to the number (=), comma, double quotes, slash, question mark, @ symbol, colon, no semicolon can be cookie content. This is why we set the contents of the cookie in the example "Test_Content". Although the restrictions are relaxed in the provision of Cookie Version 1, these characters can be used, but considering the new version of the cookie specification is still not supported for all browsers, so we should try to do it in the content of the cookie. Avoid using these characters. (Karry / Volkswagen News)