Implement Cookie in Java SE 6

Source: Internet
Author: User
Tags rfc

Source: http://www.study-code.com/java/j2se/66422.htm

 

For the first time, in order to implement the Cookie function in Java, I read several standard documents and wrote a lot of code to handle it. I saw the article today to know that Java SE 6 has provided several ready-made classes to implement the Cookie function. In the future, I will be blessed if I want to write cookies again.

Note: The following content is taken from the "Java SE 6 new feature: HTTP enhancement" section on developerworks. Address: http://www.ibm.com/developerworks/cn/java/j-lo-jse62/index.html

Cookie is a very common technology used in Web applications to store certain user information. Although we cannot store some particularly sensitive information in cookies, cookies can still help us store some trivial information and help Web users get a better experience when accessing webpages, for example, personal search parameters, color preferences, and last access time. Network Program developers can use cookies to create Stateful sessions ). Cookie applications are becoming more and more common. In Windows, we can find the Cookie used by IE in the "Documents And Settings" folder. If the user name is admin, In the Cookies folder of the admin folder, we can see some files named "admin @ (domain)". The domain indicates the network domain for creating these Cookie files, and some user information is stored in the file.

Scripting languages such as JavaScript have good support for cookies. . NET also has related classes to support developers to manage cookies. However, Java has never provided Cookie management before Java SE 6. In Java SE 5, the java.net package contains a CookieHandler abstract class, but no specific implementation is provided. In Java SE 6, the Cookie-related management class is implemented in the Java class library. With the classes supported by these cookies, Java developers can perform Cookie operations in Server programming to better support HTTP-related applications and create stateful HTTP sessions.

Use HttpCookie to represent cookies
The java.net. HttpCookie class is a new class added in Java SE 6 to indicate HTTP cookies. Its objects can represent Cookie content and support all three Cookie specifications:

Draft Netscape
RFC 2109-http://www.ietf.org/rfc/rfc2109.txt
RFC 2965-http://www.ietf.org/rfc/rfc2965.txt
This class stores information such as the Cookie name, path, value, Protocol version number, expiration date, network domain, and maximum life cycle.

Use CookiePolicy to specify Cookie Acceptance Policies
The java.net. CookiePolicy interface can specify the Cookie acceptance policy. The only method is used to determine whether a specific Cookie can be accepted by a specific address. This class has three built-in implementation subclasses. One class accepts all cookies, the other rejects all cookies, and the other class accepts all cookies from the original address.

Store cookies with CookieStore
The java.net. CookieStore interface stores and retrieves cookies. When an HTTP request is sent, it stores the accepted cookies. When an HTTP response is sent, It retrieves the corresponding cookies. In addition, when a Cookie expires, it is also responsible for automatically deleting the Cookie.

Use CookieManger/CookieHandler to manage cookies
Java.net. CookieManager is the core of the entire Cookie management mechanism. It is the default implementation subclass of CookieHandler.

A CookieManager contains a CookieStore and a CookiePolicy, which respectively store cookies and specify policies. You can specify both or use the default CookieManger.

Example
The following simple example illustrates the Cookie-related management functions:

// Create a default CookieManager
CookieManager manager = new CookieManager ();

// Get rid of rules and accept all cookies
Manager. setCookiePolicy (CookiePolicy. ACCEPT_ALL );

// Save the custom CookieManager
CookieHandler. setDefault (manager );

// Get and save the new Cookie when receiving the HTTP request
HttpCookie cookie = new HttpCookie ("... (name)...", "... (value )...");
Manager. getCookieStore (). add (uri, cookie );

// When using cookies:
// Extract CookieStore
CookieStore store = manager. getCookieStore ();

// Obtain all Uris
List <URI> uris = store. getURIs ();
For (URI uri: uris ){
// Filter the required URI
// Obtain all cookies belonging to this URI
List <HttpCookie> cookies = store. get (uri );
For (HttpCookie cookie: cookies ){
// Retrieve the Cookie
}
}

// Or, retrieve all the cookies in this CookieStore.
// Expired cookies will be automatically deleted
List <HttpCookie> cookies = store. getCookies ();
For (HttpCookie cookie: cookies ){
// Retrieve the Cookie
}

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.