How to read and write cookies with JSP

Source: Internet
Author: User
Tags versions

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";
Cookies cookie=new Cookies (cookiename, "test_content");
Cookie.setmaxage (10);
Response.addcookie (cookie);
%>
...... (Other content)
</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
Read out Cookies

After the cookie file is created, nature still needs us to read it out, otherwise we're not wasting our energy? Let's look at how to read the cookies on the user's hard disk.

<body>
<table border=1>
<tr><td>Name</td><td>value</td></tr>
<%
Cookie cookies[]=request.getcookies ();
Cookie Scookie=null;
String Svalue=null;
String Sname=null;
for (int i=0;i<cookies.length;i++)
{
Scookie=cookies[i];
Svalue=scookie.getvalue ();
Sname=scookie.getname ();
%>
<tr><td><%=sname%></td><td><%=svalue%></td></tr>
<%
}
%>
</table> ..... (Other content)
</body>

This small section of JSP files can read all valid cookies on the user's hard disk, that is, the cookie file that is still in the lifetime. Lists the names and contents of each cookie in the form of a table.

Let's analyze This code line by row:

Cookie cookies[]=request.getcookies () we read the cookie on the user's hard disk with request.getcookies () and put all the cookies in a cookie object array.

Next we use a loop to iterate through the array of cookie objects we just created, and we use scookie=cookies[i to remove a cookie object from the array. Then we use the Scookie.getvalue () and Scookie.getname () two methods to get the name and content of the cookie.


By placing the name and content of the cookie in a string variable, we can do a variety of things with it. In the example above, all cookies can be displayed in a table by looping through the loops of the statement.

Some of the issues that need to be noted

Through the above two simple examples, you can see that the use of JSP cookies to operate, is very simple. However, we also need to pay attention to some problems in the actual operation:

1. Cookie compatibility issues

The cookie format has 2 different versions, the first version, which we call Cookie version 0, was originally developed by Netscape and is supported by almost all browsers. The newer version, Cookie version 1, is based on the RFC 2109 document. To ensure compatibility, Java provides that the previously mentioned operations involving cookies are for older versions of cookies. The new version of the cookie is not currently supported by the Javax.servlet.http.Cookie package.

2. Content of Cookies

The character limit for the contents of the same cookie is different for different cookie versions. In Cookie Version 0, some special characters, such as spaces, brackets, parentheses, equal numbers (=), commas, double quotes, slashes, question marks, @ symbols, colons, and semicolons, cannot be the contents of a cookie. That's why we set the cookie's content to "test_content" in the example.

Although in cookies
The restrictions are relaxed in version 1 and can be used, but given that the new version of the cookie specification is still not supported by all browsers, it is safe to try to avoid using these characters in the contents of the cookie

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.