Java
  Home arrow Java arrow Page 2 - Saving Client State with Cookies and J...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Saving Client State with Cookies and Java
By: Kulvir Singh Bhogal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 46
    2004-03-24

    Table of Contents:
  • Saving Client State with Cookies and Java
  • Some Background Information About Cookies
  • Setting Cookies on Your Client
  • Grabbing Cookies From Your Client
  • Throwing JavaScript Into the (Cookie Dough) Mix
  • Cleaning Up: Getting Rid of Cookie Crumbs

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    Saving Client State with Cookies and Java - Some Background Information About Cookies


    (Page 2 of 6 )

    Before we delve deeper into the handling of cookies, let’s cover some basic background information about the subject.  Java Servlets (which reside on the server) send cookies to clients by adding fields to their HTTP response headers.  Similarly, clients return cookies to servers by adding fields to HTTP request headers.  When a client application (e.g., a Web browser) receives a cookie from a web server, the client application stores the cookie locally. Upon subsequent request to the web server, the cookie will be returned.  You can learn more about cookies by reading the Netscape specification (see resources section).

    Client State with Cookies in Java

    Figure 1: Cookie Transmission Over the Network 

    The server can send multiple cookies to the client.  Each cookie is sent as a separate response header.  The same is true for the client talking back to the server, except that there, we are dealing with multiple request headers.  It is important to note that you can have cookies with the same name.  For example, I might have an application where I send the server two cookies with headers both named Citizenship.    Let’s say I have dual citizenship; in such a case, I might send two cookies with two different country names as values. 

    What Can I Store in A Cookie?

    The javax.servlet.http.Cookie object allows you to set a name and an associated value.  Also, it allows you to set a comment as an optional attribute.  You can see the setters associate to the Cookie object in the figure below.  Refer to the javadoc of the javax.servlet.http.Cookie class for a complete breakdown of the class’s API.

    Client State with Cookies in Java

    Figure 2: The Setters Associated to the CookieObject 

    When you call the constructor of a Cookie, you set its name and value.  The name of the cookie must be an HTTP/1.1 “token.”  A token is a string that does not contain characters listed in RFC 2068.  Your safe bet is to use an alphanumeric string as your token.  Values of your cookie can be any string.  However, if you want to be prudent and stick to specification, the original Netscape cookie specification prohibits the use of the following characters:

    [ ] ( ) = , " / ? @ : ;

    Note that you can override the value you set for a cookie via the constructor later by calling the Cookie object’s  setValue method. 

    Studying Some Working Code

    The code mentioned throughout this article for demonstrative purposes can be downloaded here.  The code is bundled as an enterprise application archive (EAR) so you can deploy it to your application server of choice. 

    More Java Articles
    More By Kulvir Singh Bhogal


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway