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

Grabbing Cookies From Your Client - 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



Extracting cookies from your client is also a straightforward process, courtesy of the Java APIs provided to us.  The acquisition process of the cookie we set using the CookieSetterServlet is shown in Listing 2. 

import java.io.IOException;
import java
import javax
import javax
import javax
import javax
import javax
import javax
public class 
extends HttpServlet
implements Servlet
public void doGet(
  HttpServletRequest request
  HttpServletResponse response
  throws ServletException
// grab the user's favorite cookie type from the request
  String favoriteCookieType =
  // report the value to the console
   "Setting favorite cookie type to: "
    + favoriteCookieType);
  // create a new Cookie object.  
  // set the token name to "FavoriteCookieType"
  // set the value to the value extracted from the request
  Cookie favoriteCookie =
   new Cookie(
  // set the value of the comment
   "Houses User's Favorite Cookie Type");
  // add the cookie to the response
  // provide some visual feedback to the user
  PrintWriter out =
   " "
    + "");
<H2>I am now going to "remember" that you like "
    + favoriteCookieType
    + " 
cookiesusing Cookies.</H2>");
 public void doPost(
  HttpServletRequest request,
  HttpServletResponse response)
  throws ServletException, IOException
  doGet(request, response);

Listing 2: CookieSetterServlet.java

To obtain the cookie from the client, we obtain our cookies as an array of Cookie objects from the Request object.   We then have to cycle through the array, looking for the name of the cookie of our interest.  Once we find the correct cookie, we extract its value and present the value to the user (see Figure 5).

Client State with Cookies in Java

Figure 5: Output of the CookieGrabberServlet, which
Reports the Value of the Cookie Seen on the Client 

>>> 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: