Use JSP to operate cookies

Source: Internet
Author: User

Cookie should be a technology that has been used for a long time. As early as the advent of HTML, there was no way to record and identify different users between each independent page. Later, people invented the Cookie technology. When a user accesses a webpage, it can create a file on the visitor's machine. We call it a Cookie and write a piece of content into it, to identify different users. If the next time the user visits the webpage, the user can read the content in the file again, so that the webpage knows that the user has visited the webpage.

Although the current web page production technology has developed much more than a few years ago. However, sometimes cookies can help us a lot. Next, let's take a look at how to use JSP to operate cookies when writing JSP files.

Ii. Write Cookie

In fact, using JSP to operate cookies is very simple. Let's look at the following JSP program:

... (Omitted in the middle)


<%
String cookieName = "Sender ";
Cookie cookie = new Cookie (cookieName, "Test_Content ");
Cookie. setMaxAge (10 );
Response. addCookie (cookie );
%>
...... (Other content)


In this way, we set a Cookie. Is it easy?

Let's take a closer look at this Code:

Cookie cookie = new Cookie (cookieName, "Test_Content ");

This line creates a Cookie object with two initialization parameters. The first parameter cookieName defines the Cookie name, and the last parameter is also a string that defines the Cookie content. That is, the file content we want the webpage to identify on the user's machine.

Next line: cookie. setMaxAge (10). The setMaxAge method in the Cookie is called to set the Cookie storage duration to 10 seconds on the hard disk of the user's machine. A Cookie does not exist on the user's hard disk for an indefinite period of time. When a Cookie object is created, we must specify the Cookie retention period. After this period is exceeded, cookie files will no longer work and will be deleted by your browser. If we hope that the Cookie file will still be valid and can be read from the webpage when users access this page next time, we can set the Cookie retention period to a little longer. For example, cookie. setMaxAge (365*24*60*60) can make the Cookie file valid within one year.

3. read cookies

After the Cookie file is created, we still need to read it. Otherwise, isn't it a waste of effort? Next, let's take a look at how to read the cookies on the user's hard disk.

... (Omitted in the middle)

Name value

<%
Cookie cookies [] = request. getCookies ();
Cookie sCookie = null;
String svalue = null;
String sname = null;
For (int I = 0; I {
SCookie = cookies [I];
Svalue = sCookie. getValue ();
Sname = sCookie. getName ();
%>

<%
}
%>

Name value
<% = Name %> <% = svalue %>

...... (Other content)

This small JSP file can read all valid cookies on the user's hard disk, that is, the Cookie files that are still in use. List the names and contents of each Cookie in a table.

Let's analyze this code line by line:

Cookie cookies [] = request. getCookies () We use request. getCookies () to read the cookies on the user's hard disk and put all the cookies in an array of Cookie objects.

Next, we use a circular statement to traverse the Cookie object array we just created. We use sCookie = cookies [I] to retrieve a Cookie object in the array, and then we use sCookie. getValue () and sCookie. getName () is used to obtain the Cookie name and content.

By placing the obtained Cookie name and content in the string variable, we can perform various operations on it. In the preceding example, all cookies can be displayed in a table through loop statement traversal.

4. Notes

The preceding two simple examples show that it is very simple to use JSP to perform Cookie operations. However, we should pay attention to the following issues in actual operations:

1. Cookie compatibility

The Cookie format has two different versions. The first Version, called Cookie Version 0, was originally developed by Netscape and is supported by almost all browsers. The newer Version, Cookie Version 1, is developed according to RFC 2109. To ensure compatibility, JAVA stipulates that all the Cookie-related operations mentioned earlier are performed on old versions of cookies. The new Cookie version is not supported by the Javax. servlet. http. Cookie package.

2. Cookie content

The character restrictions of the same Cookie content vary with different Cookie versions. In Cookie Version 0, some special characters, such as space, square brackets, Parentheses, equal signs (=), commas, double quotation marks, slashes, question marks, @ symbols, colons, the semicolon cannot be used as the Cookie content. This is why we set the Cookie content to "Test_Content" in the example.

Although these characters can be used in Cookie Version 1, the new Cookie specification is not currently supported by all browsers. Therefore, it is safe, we should avoid using these characters in Cookie content. (Karry/Public Network News)

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.