Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

A recipe for cookie management

Integrate an easy-to-use library for client-side cookie handling

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

While developing a universal email client offering single-point access to all major Internet mail services, Web-based or otherwise, I found my application often had to act as a mini Web browser to interact with mail provider Websites. I stumbled upon the same need for Website interaction while developing XML Web service implementations to facilitate machine access to Websites. These sites often use cookies for state management—that is, to maintain user session data. In both cases, I realized that most Website interaction logic dealt with cookie handling. I also noticed that although both applications performed cookie handling, the logic was quite different and not interchangeable. In response to these limitations, I set out to develop a lightweight general-purpose library devoted to cookie handling. In this article, I share this library with you.

To illustrate the library in action, I present a console-based Hotmail email checker. Further, I explore client-side state management from the mobile device perspective using the Mobile Information Device Profile (MIDP) from the Java 2 Platform, Micro Edition (J2ME).

Cookie basics

Lets begin by answering some questions:

  • What is state management, and why do we need it?
  • What are cookies, and how do they fit into the picture?


To answer the first question, we must examine HTTP more closely. HTTP is basically a stateless protocol because, from a Web server's perspective, all HTTP requests are independent of preceding requests. That means each HTTP response depends entirely on the information contained in the corresponding request. While this behavior allows a simple and efficient Web server implementation, using it as a basis for complex Web applications proves rather difficult.

The state management mechanism overcomes this HTTP limitation and allows Web clients and servers to maintain a relationship between requests. The period during which this relationship holds is called a session. Most Web applications that require you to log in use sessions and state management. Shopping cart applications use state management to hold a list of all items marked for purchase. State management enables customization of portals and search engines to a particular user's preferences. Web applications can even use state management to tailor Website content based on user interests.

Cookies affect state management. Cookies are small pieces of text stored by the server on the local machine and sent with every request to that same server. The IETF RFC 2965 HTTP State Management Mechanism is the current cookie specification. The Web server uses HTTP headers, specified in the RFC (request for comments), to send cookies to the client. At the client end, the browser parses these cookies and stores them in a local file. It then attaches these cookies automatically to any requests to the same server. In the remainder of this article, I use the terms cookie handling and state management synonymously.

If you want to find out which sites you visit use cookies, try this simple experiment:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources