First, in the foreword, cookie should be an application for a long time. 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 production technology of the web page has been 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 to operate cookies is very simple, let's look at the following JSP program:
........ (middle) head> <% String cookiename = "sender"; cookie cookie = new cookie (cookiename, "test_content"); cookie.setMaxage (10); response.addcookie (cookie);%> ........ (other content) Body> html> So we set a cookie, very simple? Let's take a closer study: cookie cookie = new cookie (cookiename, "test_content"); this line establishes a cookie object, initializing two parameters, the first parameter cookiename defines the name of the cookie, the latter parameter It 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), call the setMaxage method in cookies, setting the Survival period on the user's machine hard disk is 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. Third, after reading the cookie cookie file, naturally, we 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) head>