Home arrow Java & J2EE arrow Page 5 - Saving Client State with Cookies and Java

Throwing JavaScript Into the (Cookie Dough) Mix - Java

Cookies serve as a facility for servers to send information to a client.†† This information is then housed on the client, from which the server can later retrieve the information.†In this article, we will study the concept of saving client state with cookies using Java Servlets.†Iíll walk you through an end to end example where you will store and retrieve data using cookies.

  1. Saving Client State with Cookies and Java
  2. Some Background Information About Cookies
  3. Setting Cookies on Your Client
  4. Grabbing Cookies From Your Client
  5. Throwing JavaScript Into the (Cookie Dough) Mix
  6. Cleaning Up: Getting Rid of Cookie Crumbs
By: Kulvir Singh Bhogal
Rating: starstarstarstarstar / 57
March 24, 2004

print this article



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. 

<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)
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;
// -->
<h2>According to JavaScript, you like <script language="JavaScript">
</script> Cookies .</h2>

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 & J2EE Articles          >>> More By Kulvir Singh Bhogal

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: