Managing Cookies with CookieManager Class in J2SE Mustang
Starting with J2SE Mustang, managing cookies is made much easier, due to the new CookieManager class. This class represents an implementation of the abstract CookieHandler class (the implementation conforms to RFC 2965, section 3.3.). Now, a CookieManager object is based on storage rule (represented by a CookieStore object), and on a policy rule (represented by a CookiePolicy object), which makes policy decisions on cookie acceptance/rejection. The default storage in an internal in-memory representation.
Another important class involved in this implementation is HttpCookie. As the name suggest this class represents a HTTP cookie. Here are some of the useful methods for getting and setting a cookie:
To create a CookieManager, call one of the following two constructors:
-
public CookieManager(): This method creates a new cookie manager with a default cookie store and accept policy (similar to CookieManager(null, null)).
-
public CookieManager(CookieStore store, CookiePolicy cookiePolicy): This method creates a new cookie manager with the specified storage and policy rule.
The policy rule can be:
-
static final CookiePolicy ACCEPT_ALL: Accepts all cookies.
-
static final CookiePolicy ACCEPT_ORIGINAL_SERVER: Accepts cookies only from the original server.
-
static final CookiePolicy ACCEPT_NONE: Accepts no cookies.
To customize a policy rule, you have to extend the CookiePolicy interface and implement the shouldAccept method, which decides if a cookie must be accepted/rejected:
boolean shouldAccept(URI uri, HttpCookie cookie)
To customize storage, you have to extend the CookieStore interface and implement the following methods:
-
void add(URI uri, HttpCookie cookie): This method adds a cookie to the store.
-
List<HttpCookie> get(URI uri): This method retrieves cookies associated with given URI.
-
List<HttpCookie> getCookies(): This method retrieves all not-expired cookies in the cookie store.
-
List<URI> getURIs(): This method retreives all URIs which identify the cookies in this cookie store.
-
boolean remove(URI uri, HttpCookie cookie): This method removes a cookie from the store.
-
boolean removeAll(): This method removes all cookies in this cookie store.
Listing 8 shows you how to use the default CookieHandler implementation to extract the cookie from an HTTP response.
Everyone Likes Cookies
Since all the major Web browsers support cookies, a Web client must be able to manage them. If you are about to write a J2SE application that will act as a Web client, this article can help you solve your cookie management problems. A solid cookie management implementation can help make your Web a trusted partner for a Web server, since many servers presume that a Web client knows how to deal with cookies.
| Home / Articles
/ Recipes for Cookie Management in J2SEs Tiger and Mustang / 1 / 2 / 3 / 4 / 5 / |
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|