com.oreilly.servlet
Class CookieParser

java.lang.Object
  extended by com.oreilly.servlet.CookieParser

public class CookieParser
extends java.lang.Object

A class to simplify cookie retrieval. It can retrieve cookie values by name and return the value as any primitive type (no casting or parsing required). It can also throw an exception when a cookie is not found (simplifying error handling), and can accept default values (eliminating error handling).

It is used like this:

 CookieParser parser = new CookieParser(req);
  
 float ratio = parser.getFloatCookie("ratio", 1.0);
  
 int count = 0;
 try {
   count = parser.getIntCookie("count");
 }
 catch (NumberFormatException e) {
   handleMalformedCount();
 }
 catch (CookieNotFoundException e) {
   handleNoCount();
 }
 

Version:
1.0, 2000/03/19
Author:
Jason Hunter, Copyright © 2000
See Also:
CookieNotFoundException

Constructor Summary
CookieParser(HttpServletRequest req)
          Constructs a new CookieParser to handle the cookies of the given request.
 
Method Summary
 boolean getBooleanCookie(java.lang.String name)
          Gets the named cookie value as a boolean
 boolean getBooleanCookie(java.lang.String name, boolean def)
          Gets the named cookie value as a boolean, with a default.
 byte getByteCookie(java.lang.String name)
          Gets the named cookie value as a byte
 byte getByteCookie(java.lang.String name, byte def)
          Gets the named cookie value as a byte, with a default.
 char getCharCookie(java.lang.String name)
          Gets the named cookie value as a char
 char getCharCookie(java.lang.String name, char def)
          Gets the named cookie value as a char, with a default.
 double getDoubleCookie(java.lang.String name)
          Gets the named cookie value as a double
 double getDoubleCookie(java.lang.String name, double def)
          Gets the named cookie value as a double, with a default.
 float getFloatCookie(java.lang.String name)
          Gets the named cookie value as a float
 float getFloatCookie(java.lang.String name, float def)
          Gets the named cookie value as a float, with a default.
 int getIntCookie(java.lang.String name)
          Gets the named cookie value as a int
 int getIntCookie(java.lang.String name, int def)
          Gets the named cookie value as a int, with a default.
 long getLongCookie(java.lang.String name)
          Gets the named cookie value as a long
 long getLongCookie(java.lang.String name, long def)
          Gets the named cookie value as a long, with a default.
 short getShortCookie(java.lang.String name)
          Gets the named cookie value as a short
 short getShortCookie(java.lang.String name, short def)
          Gets the named cookie value as a short, with a default.
 java.lang.String getStringCookie(java.lang.String name)
          Gets the named cookie value as a String
 java.lang.String getStringCookie(java.lang.String name, java.lang.String def)
          Gets the named cookie value as a String, with a default.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CookieParser

public CookieParser(HttpServletRequest req)
Constructs a new CookieParser to handle the cookies of the given request.

Parameters:
req - the servlet request
Method Detail

getStringCookie

public java.lang.String getStringCookie(java.lang.String name)
                                 throws CookieNotFoundException
Gets the named cookie value as a String

Parameters:
name - the cookie name
Returns:
the cookie value as a String
Throws:
CookieNotFoundException - if the cookie was not found

getStringCookie

public java.lang.String getStringCookie(java.lang.String name,
                                        java.lang.String def)
Gets the named cookie value as a String, with a default. Returns the default value if the cookie is not found

Parameters:
name - the cookie name
def - the default cookie value
Returns:
the cookie value as a String, or the default

getBooleanCookie

public boolean getBooleanCookie(java.lang.String name)
                         throws CookieNotFoundException
Gets the named cookie value as a boolean

Parameters:
name - the cookie name
Returns:
the cookie value as a boolean
Throws:
CookieNotFoundException - if the cookie was not found

getBooleanCookie

public boolean getBooleanCookie(java.lang.String name,
                                boolean def)
Gets the named cookie value as a boolean, with a default. Returns the default value if the cookie is not found.

Parameters:
name - the cookie name
def - the default cookie value
Returns:
the cookie value as a boolean, or the default

getByteCookie

public byte getByteCookie(java.lang.String name)
                   throws CookieNotFoundException,
                          java.lang.NumberFormatException
Gets the named cookie value as a byte

Parameters:
name - the cookie name
Returns:
the cookie value as a byte
Throws:
CookieNotFoundException - if the cookie was not found
java.lang.NumberFormatException - if the cookie value could not be converted to a byte

getByteCookie

public byte getByteCookie(java.lang.String name,
                          byte def)
Gets the named cookie value as a byte, with a default. Returns the default value if the cookie is not found or cannot be converted to a byte.

Parameters:
name - the cookie name
def - the default cookie value
Returns:
the cookie value as a byte, or the default

getCharCookie

public char getCharCookie(java.lang.String name)
                   throws CookieNotFoundException
Gets the named cookie value as a char

Parameters:
name - the cookie name
Returns:
the cookie value as a char
Throws:
CookieNotFoundException - if the cookie was not found

getCharCookie

public char getCharCookie(java.lang.String name,
                          char def)
Gets the named cookie value as a char, with a default. Returns the default value if the cookie is not found.

Parameters:
name - the cookie name
def - the default cookie value
Returns:
the cookie value as a char, or the default

getDoubleCookie

public double getDoubleCookie(java.lang.String name)
                       throws CookieNotFoundException,
                              java.lang.NumberFormatException
Gets the named cookie value as a double

Parameters:
name - the cookie name
Returns:
the cookie value as a double
Throws:
CookieNotFoundException - if the cookie was not found
java.lang.NumberFormatException - if the cookie could not be converted to a double

getDoubleCookie

public double getDoubleCookie(java.lang.String name,
                              double def)
Gets the named cookie value as a double, with a default. Returns the default value if the cookie is not found.

Parameters:
name - the cookie name
def - the default cookie value
Returns:
the cookie value as a double, or the default

getFloatCookie

public float getFloatCookie(java.lang.String name)
                     throws CookieNotFoundException,
                            java.lang.NumberFormatException
Gets the named cookie value as a float

Parameters:
name - the cookie name
Returns:
the cookie value as a float
Throws:
CookieNotFoundException - if the cookie was not found
java.lang.NumberFormatException - if the cookie could not be converted to a float

getFloatCookie

public float getFloatCookie(java.lang.String name,
                            float def)
Gets the named cookie value as a float, with a default. Returns the default value if the cookie is not found.

Parameters:
name - the cookie name
def - the default cookie value
Returns:
the cookie value as a float, or the default

getIntCookie

public int getIntCookie(java.lang.String name)
                 throws CookieNotFoundException,
                        java.lang.NumberFormatException
Gets the named cookie value as a int

Parameters:
name - the cookie name
Returns:
the cookie value as a int
Throws:
CookieNotFoundException - if the cookie was not found
java.lang.NumberFormatException - if the cookie could not be converted to a int

getIntCookie

public int getIntCookie(java.lang.String name,
                        int def)
Gets the named cookie value as a int, with a default. Returns the default value if the cookie is not found.

Parameters:
name - the cookie name
def - the default cookie value
Returns:
the cookie value as a int, or the default

getLongCookie

public long getLongCookie(java.lang.String name)
                   throws CookieNotFoundException,
                          java.lang.NumberFormatException
Gets the named cookie value as a long

Parameters:
name - the cookie name
Returns:
the cookie value as a long
Throws:
CookieNotFoundException - if the cookie was not found
java.lang.NumberFormatException - if the cookie could not be converted to a long

getLongCookie

public long getLongCookie(java.lang.String name,
                          long def)
Gets the named cookie value as a long, with a default. Returns the default value if the cookie is not found.

Parameters:
name - the cookie name
def - the default cookie value
Returns:
the cookie value as a long, or the default

getShortCookie

public short getShortCookie(java.lang.String name)
                     throws CookieNotFoundException,
                            java.lang.NumberFormatException
Gets the named cookie value as a short

Parameters:
name - the cookie name
Returns:
the cookie value as a short
Throws:
CookieNotFoundException - if the cookie was not found
java.lang.NumberFormatException - if the cookie could not be converted to a short

getShortCookie

public short getShortCookie(java.lang.String name,
                            short def)
Gets the named cookie value as a short, with a default. Returns the default value if the cookie is not found.

Parameters:
name - the cookie name
def - the default cookie value
Returns:
the cookie value as a short, or the default