Java
  Home arrow Java arrow Page 6 - Saving Client State with Cookies and Java
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
JAVA

Saving Client State with Cookies and Java
By: Kulvir Singh Bhogal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 52
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - 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



     
     
    >>> More Java Articles          >>> More By Kulvir Singh Bhogal
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek