Java
  Home arrow Java arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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 / 48
    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


    Saving Client State with Cookies and Java - Grabbing Cookies From Your Client


    (Page 4 of 6 )

    Extracting cookies from your client is also a straightforward process, courtesy of the Java APIs provided to us.  The acquisition process of the cookie we set using the CookieSetterServlet is shown in Listing 2. 


    import java.io.IOException;
    import java
    .io.PrintWriter;
    import javax
    .servlet.Servlet;
    import javax
    .servlet.ServletException;
    import javax
    .servlet.http.Cookie;
    import javax
    .servlet.http.HttpServlet;
    import javax
     
    .servlet
     
    .http
     
    .HttpServletRequest;
    import javax
     
    .servlet
     
    .http
     
    .HttpServletResponse;
     
    public class 
    CookieSetterServlet
     
    extends HttpServlet
     
    implements Servlet
    {
     
    public void doGet(
      HttpServletRequest request
    ,
      HttpServletResponse response
    )
      throws ServletException
    IOException
     
    {
      
    // grab the user's favorite cookie type from the request
      String favoriteCookieType =
       request.getParameter(
        "FavoriteCookiePreference");
      // report the value to the console
      System.out.println(
       "Setting favorite cookie type to: "
        + favoriteCookieType);
      // create a new Cookie object.  
      // set the token name to "FavoriteCookieType"
      // set the value to the value extracted from the request
      Cookie favoriteCookie =
       new Cookie(
        "FavoriteCookieType",
        favoriteCookieType);
      // set the value of the comment
      favoriteCookie.setComment(
       "Houses User's Favorite Cookie Type");
      // add the cookie to the response
      response.addCookie(
       favoriteCookie);
      // provide some visual feedback to the user
      PrintWriter out =
       response.getWriter();
      out.println(
       " "
        + "");
      out.println(
       " 
    <H2>I am now going to "remember" that you like "
        + favoriteCookieType
        + " 
    cookiesusing Cookies.</H2>");
      out.println("");
     }
     public void doPost(
      HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException
     {
      doGet(request, response);
     }
    }

    Listing 2: CookieSetterServlet.java

    To obtain the cookie from the client, we obtain our cookies as an array of Cookie objects from the Request object.   We then have to cycle through the array, looking for the name of the cookie of our interest.  Once we find the correct cookie, we extract its value and present the value to the user (see Figure 5).

    Client State with Cookies in Java

    Figure 5: Output of the CookieGrabberServlet, which
    Reports the Value of the Cookie Seen on the Client 

    More Java Articles
    More By Kulvir Singh Bhogal


     

       

    JAVA ARTICLES

    - New Object Initialization in Java
    - 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





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