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 take a look at the following section of JSPProgram:
... (Omitted in the middle)
<%
String cookiename = "sender ";
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 section.Code:
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)