Manipulating cookies with JSP

Source: Internet
Author: User
Tags valid versions
Cookie|js


The cookie should be a technology that is 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 now 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 cookies

In fact, using JSP to manipulate cookies is very simple, we look at the following section of the JSP program:

..... (middle of slightly)


<%
String cookiename= "Sender";
Cookies cookie=new Cookies (cookiename, "test_content");
Cookie.setmaxage (10);
Response.addcookie (cookie);
%>
........ (Other content)


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.

The 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.

Three, read out cookies

cookie file is created, nature still need us to read it out, otherwise we will not waste energy? Let's look at how to read the cookies on the user's hard disk.

..... (middle of slightly)

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 section of the JSP file can read out all the 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 cookies 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 over the cookie object array we just created, we use scookie=cookies[i to remove a cookie object from the array, and then we use Scookie.getvalue () and Scookie.getname ( Two methods to get the name and content of this cookie.

By placing the name and content of the cookie in a string variable, we can perform various operations on it. In the example above, all cookies can be displayed in a table by looping through the loops of the statement.

Some problems needing attention

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. Compatibility issues with cookies

The cookie format has 2 different versions, the first version, which we call Cookie version 0, which was originally developed by Netscape and 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. The contents of the cookie

The character limits for the contents of the same cookie are 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 it is possible to use these characters in the cookie version 1 rule, we should try to avoid using these characters in the contents of cookies, given that the new version of the cookie specification is still not supported by all browsers. (karry/public network newspaper)





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.