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();
 }
 

版本:
1.0, 2000/03/19
作者:
Jason Hunter, Copyright © 2000
另请参见:
CookieNotFoundException

构造方法摘要
CookieParser(javax.servlet.http.HttpServletRequest req)
          Constructs a new CookieParser to handle the cookies of the given request.
 
方法摘要
 boolean getBooleanCookie(String name)
          Gets the named cookie value as a boolean
 boolean getBooleanCookie(String name, boolean def)
          Gets the named cookie value as a boolean, with a default.
 byte getByteCookie(String name)
          Gets the named cookie value as a byte
 byte getByteCookie(String name, byte def)
          Gets the named cookie value as a byte, with a default.
 char getCharCookie(String name)
          Gets the named cookie value as a char
 char getCharCookie(String name, char def)
          Gets the named cookie value as a char, with a default.
 double getDoubleCookie(String name)
          Gets the named cookie value as a double
 double getDoubleCookie(String name, double def)
          Gets the named cookie value as a double, with a default.
 float getFloatCookie(String name)
          Gets the named cookie value as a float
 float getFloatCookie(String name, float def)
          Gets the named cookie value as a float, with a default.
 int getIntCookie(String name)
          Gets the named cookie value as a int
 int getIntCookie(String name, int def)
          Gets the named cookie value as a int, with a default.
 long getLongCookie(String name)
          Gets the named cookie value as a long
 long getLongCookie(String name, long def)
          Gets the named cookie value as a long, with a default.
 short getShortCookie(String name)
          Gets the named cookie value as a short
 short getShortCookie(String name, short def)
          Gets the named cookie value as a short, with a default.
 String getStringCookie(String name)
          Gets the named cookie value as a String
 String getStringCookie(String name, String def)
          Gets the named cookie value as a String, with a default.
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

CookieParser

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

参数:
req - the servlet request
方法详细信息

getStringCookie

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

参数:
name - the cookie name
返回:
the cookie value as a String
抛出:
CookieNotFoundException - if the cookie was not found

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

参数:
name - the cookie name
def - the default cookie value
返回:
the cookie value as a String, or the default

getBooleanCookie

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

参数:
name - the cookie name
返回:
the cookie value as a boolean
抛出:
CookieNotFoundException - if the cookie was not found

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.

参数:
name - the cookie name
def - the default cookie value
返回:
the cookie value as a boolean, or the default

getByteCookie

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

参数:
name - the cookie name
返回:
the cookie value as a byte
抛出:
CookieNotFoundException - if the cookie was not found
NumberFormatException - if the cookie value could not be converted to a byte

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.

参数:
name - the cookie name
def - the default cookie value
返回:
the cookie value as a byte, or the default

getCharCookie

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

参数:
name - the cookie name
返回:
the cookie value as a char
抛出:
CookieNotFoundException - if the cookie was not found

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.

参数:
name - the cookie name
def - the default cookie value
返回:
the cookie value as a char, or the default

getDoubleCookie

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

参数:
name - the cookie name
返回:
the cookie value as a double
抛出:
CookieNotFoundException - if the cookie was not found
NumberFormatException - if the cookie could not be converted to a double

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.

参数:
name - the cookie name
def - the default cookie value
返回:
the cookie value as a double, or the default

getFloatCookie

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

参数:
name - the cookie name
返回:
the cookie value as a float
抛出:
CookieNotFoundException - if the cookie was not found
NumberFormatException - if the cookie could not be converted to a float

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.

参数:
name - the cookie name
def - the default cookie value
返回:
the cookie value as a float, or the default

getIntCookie

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

参数:
name - the cookie name
返回:
the cookie value as a int
抛出:
CookieNotFoundException - if the cookie was not found
NumberFormatException - if the cookie could not be converted to a int

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.

参数:
name - the cookie name
def - the default cookie value
返回:
the cookie value as a int, or the default

getLongCookie

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

参数:
name - the cookie name
返回:
the cookie value as a long
抛出:
CookieNotFoundException - if the cookie was not found
NumberFormatException - if the cookie could not be converted to a long

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.

参数:
name - the cookie name
def - the default cookie value
返回:
the cookie value as a long, or the default

getShortCookie

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

参数:
name - the cookie name
返回:
the cookie value as a short
抛出:
CookieNotFoundException - if the cookie was not found
NumberFormatException - if the cookie could not be converted to a short

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.

参数:
name - the cookie name
def - the default cookie value
返回:
the cookie value as a short, or the default


Copyright © 2013. All Rights Reserved.