All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.oreilly.servlet.CookieParser

java.lang.Object
   |
   +----com.oreilly.servlet.CookieParser

public class CookieParser
extends 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 Index

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

Method Index

 o getBooleanCookie(String)
Gets the named cookie value as a boolean
 o getBooleanCookie(String, boolean)
Gets the named cookie value as a boolean, with a default.
 o getByteCookie(String)
Gets the named cookie value as a byte
 o getByteCookie(String, byte)
Gets the named cookie value as a byte, with a default.
 o getCharCookie(String)
Gets the named cookie value as a char
 o getCharCookie(String, char)
Gets the named cookie value as a char, with a default.
 o getDoubleCookie(String)
Gets the named cookie value as a double
 o getDoubleCookie(String, double)
Gets the named cookie value as a double, with a default.
 o getFloatCookie(String)
Gets the named cookie value as a float
 o getFloatCookie(String, float)
Gets the named cookie value as a float, with a default.
 o getIntCookie(String)
Gets the named cookie value as a int
 o getIntCookie(String, int)
Gets the named cookie value as a int, with a default.
 o getLongCookie(String)
Gets the named cookie value as a long
 o getLongCookie(String, long)
Gets the named cookie value as a long, with a default.
 o getShortCookie(String)
Gets the named cookie value as a short
 o getShortCookie(String, short)
Gets the named cookie value as a short, with a default.
 o getStringCookie(String)
Gets the named cookie value as a String
 o getStringCookie(String, String)
Gets the named cookie value as a String, with a default.

Constructors

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

Parameters:
req - the servlet request

Methods

 o getStringCookie
 public String getStringCookie(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
 o getStringCookie
 public String getStringCookie(String name,
                               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
 o getBooleanCookie
 public boolean getBooleanCookie(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
 o getBooleanCookie
 public boolean getBooleanCookie(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
 o getByteCookie
 public byte getByteCookie(String name) throws CookieNotFoundException, 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
Throws: NumberFormatException
if the cookie value could not be converted to a byte
 o getByteCookie
 public byte getByteCookie(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
 o getCharCookie
 public char getCharCookie(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
 o getCharCookie
 public char getCharCookie(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
 o getDoubleCookie
 public double getDoubleCookie(String name) throws CookieNotFoundException, 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
Throws: NumberFormatException
if the cookie could not be converted to a double
 o getDoubleCookie
 public double getDoubleCookie(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
 o getFloatCookie
 public float getFloatCookie(String name) throws CookieNotFoundException, 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
Throws: NumberFormatException
if the cookie could not be converted to a float
 o getFloatCookie
 public float getFloatCookie(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
 o getIntCookie
 public int getIntCookie(String name) throws CookieNotFoundException, 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
Throws: NumberFormatException
if the cookie could not be converted to a int
 o getIntCookie
 public int getIntCookie(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
 o getLongCookie
 public long getLongCookie(String name) throws CookieNotFoundException, 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
Throws: NumberFormatException
if the cookie could not be converted to a long
 o getLongCookie
 public long getLongCookie(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
 o getShortCookie
 public short getShortCookie(String name) throws CookieNotFoundException, 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
Throws: NumberFormatException
if the cookie could not be converted to a short
 o getShortCookie
 public short getShortCookie(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

All Packages  Class Hierarchy  This Package  Previous  Next  Index