I. Introduction
To put it, cookie should be a long-lasting technology. 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 to cookie
In fact, using JSP operation cookies is very simple, let's see a JSP program below:
........ (middle) head> <% string cookiename = "sender"; cookie cookie = new cookie (cookiename, "test_content"); cookie.setMaxage (10); response.addcookie (cookie);%> ........ (other content) body> html>
This way we set up a cookie, very simple?
Let's take a closer study of this code:
Cookie cookie = new cookie (cookiename, "test_content");
This line has established a cookie object. Initialization has two parameters, the first parameter cookiename defines the name of the cookie, the latter parameter, also a string, 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, read the cookie
After the cookie file is created, we naturally need to read it out, otherwise we are not white enough? Next, let's see how to read the cookie on the user's hard drive.
........ (middle) head>