SunQuest
 
       Java
  Home arrow Java arrow Page 6 - 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 
 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Saving Client State with Cookies and Java - Cleaning Up: Getting Rid of Cookie Crumbs


    (Page 6 of 6 )

    To delete a cookie, you can use the setMaxAge method of the Cookie class. Using a method argument of zero, the cookie on the client side will be effectively deleted.  This is demonstrated in CookieCrumbDeleterServlet.java which is shown in Listing 4.


    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 CookieCrumbDeleterServlet
     
    extends HttpServlet
     
    implements Servlet
    {
     
    public void doGet(
      HttpServletRequest request
    ,
      HttpServletResponse response
    )
      throws ServletException
    IOException
     
    {
      Cookie
    [] cookies =
       request
    .getCookies();
      String cookieExtract 
    null;
      Cookie cookieToDelete 
    null;
      
    for (int i 0;
       i 
    cookies.length;
       i
    ++)
      
    {
       
    if (cookies[i]
        
    .getName()
        
    .equals("FavoriteCookieType"))
       
    {
        cookieToDelete 
    =
         cookies
    [i];
        
    // mark for deletion by client by setting max age to zero
        cookieToDelete
         .setMaxAge(
         0);
       }
      }
      //  add the cookie to the response back to the client
      response.addCookie(
       cookieToDelete);
      response.setContentType(
       "text/html");
      PrintWriter out =
       response.getWriter();
      out.println(
       " "
        + "");
      out.println(
       " 
    <H2>I just deleted your cookie.</H2>");
      out.println("");
     }
     public void doPost(
      HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException
     {
      doGet(request, response);
     }
    }



    Listing 3: CookieCrumbDeleterServlet.java

    Conclusion

    Giving your clients a “memory” is a highly needed feature in many web applications.  One can leverage techniques such as persisting to a database, but in many cases, one can leverage the facility of cookies which most popular browsers support.  A word of caution though, many browsers give users the option to block cookies (see Figure below of Microsoft Internet Explorer’s Advanced Privacy Settings).

    Client State with Cookies in Java

    Figure 7: Internet Explorer Allows Users the Ability to Block Cookies 

    Consequently, when one designs their web applications and considers the user of cookie technology, they should take the ability for clients to block cookies and delete cookies into major consideration.

    Background Information: Fundamentals of Java Servlets 

    Resources
    Servlet API
    Handing Cookies Using the java.net.* API
    Netscape Cookie Specification


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    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

    BlackBerry VTS




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