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

    - 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