Java
  Home arrow Java arrow Page 5 - 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 - Throwing JavaScript Into the (Cookie Dough) Mix
    ( Page 5 of 6 )

    Since our cookies are stored on our client, our client Internet browser can access them using JavaScript.  We do just that in the file grabcookievaluewithjavascript.html whose code is shown in Listing 3 below. 


    <HTML>
    <HEAD>
    <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <META name="GENERATOR" content="IBM WebSphere Studio">
    <META http-equiv="Content-Style-Type" content="text/css">
    <LINK href="theme/Master.css" rel="stylesheet" type="text/css">
    <TITLE>JavaScript Cookie Demo</TITLE>
    <SCRIPT language="JavaScript">
    <!--
     
    function GetCookieValue(offset)
     
    {
      
    var endstr document.cookie.indexOf (";"offset);
      
    if (endstr == -1)
          endstr 
    document.cookie.length;
      document
    .write(document.cookie.substring(offsetendstr));
      
    return unescape(document.cookie.substring(offsetendstr));
     
    }
     

     
    function ReportCookieValue (cookieName
     
    {
      
    var arg cookieName "=";
        
    var alen arg.length;
      
    var clen document.cookie.length;
      
    var 0;
        
    while (clen
        
    {
         

          
    var alen;
          
    var sub document.cookie.substring(i,j);
          
    if (sub == arg)
          
    {
             
    return GetCookieValue(j);
            
    }
          
    else document.cookie.indexOf(" "i) + 1;
          
    if (== 0) break; 
          

        
    }    
        
    return null;
     
    }
    // -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <p></p>
    <h2>According to JavaScript, you like <script language="JavaScript">
     
    <!--//ReportCookieValue("FavoriteCookieType")//-->
     
    </script> Cookies .</h2>
    <p></p>
     
    </BODY>
    </HTML>

    Listing 3: grabcookiewithjavascript.html

    The output of our JavaScript cookie extraction is shown below:

    Client State with Cookies in Java

     
    Figure 6: Extracting our Cookie Value on the Client via JavaScript 

    Being able to access your cookies via JavaScript represents a powerful functionality. By doing this, you can have forms preloaded with what is resident in client cookie data. As an exercise, you should try and modify favoritecookie.html to initially present the pull down value that was resident in your cookie.



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek