How to read and write cookies with JSP

Source: Internet
Author: User
Tags valid

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.