In a word, cookies should be a technology that has been used for a long time. As early as the HTML appeared, there was no way to record and identify different users between each individual page. Later, people invented the cookie technology, when the user visited the page, it can create a file on the visitor's machine, we call it as a cookie, write a paragraph in to identify different users. If the next time a user visits this page, it can read the contents of the file, so that the page will know the last time the user has visited the page.
Although the production of web pages today than a few years ago has developed a lot. But there are times when cookies can help us a lot. Next, let's take a look at how to use JSP to manipulate cookies when writing JSP files.
Write Cookie
In fact, using JSP to manipulate cookies is very simple, we look at the following section of the JSP program:
<body>
<%
String cookieName="Sender";
Cookie cookie=new Cookie(cookieName, "Test_Content");
cookie.setMaxAge(10);
response.addCookie(cookie);
%>
……(其他内容)
</body>
So we set up a cookie, is it simple?
Let's take a closer look at this piece of code:
Cookies cookie=new Cookies (cookiename, "test_content");
This line creates a cookie object that initializes two parameters, the first parameter cookiename the name of the cookie, the last parameter, and a string that defines the contents of the cookie. That is, the contents of the file that we want the page to identify on the user's machine.
Next line: Cookie.setmaxage (10) calls the Setmaxage method in the cookie to set the cookie to survive for 10 seconds on the user's machine hard disk. A cookie in the user's hard disk is not the time to exist indefinitely, in the creation of the cookie object, we must create a cookie lifetime, beyond this lifetime, the cookie file will no longer work, will be deleted by the user's browser. If we want the cookie file to be valid and readable by the Web page the next time the user accesses the page, we can set the cookie to a slightly longer lifetime. For example, Cookie.setmaxage (365*24*60*60) can make the cookie file valid for a year.