com.oreilly.servlet
类 ParameterParser

java.lang.Object
  继承者 com.oreilly.servlet.ParameterParser

public class ParameterParser
extends Object

A class to simplify parameter handling. It can return parameters of any primitive type (no casting or parsing required), can throw an exception when a parameter is not found (simplifying error handling), and can accept default values (eliminating error handling).

It is used like this:

 ParameterParser parser = new ParameterParser(req);
  
 float ratio = parser.getFloatParameter("ratio", 1.0);
  
 int count = 0;
 try {
   count = parser.getIntParameter("count");
 }
 catch (NumberFormatException e) {
   handleMalformedCount();
 }
 catch (ParameterNotFoundException e) {
   handleNoCount();
 }
 
There's also a capability to find out if any required parameters are missing from a request:
 ParameterParser parser = new ParameterParser(req);
 String[] required = { "fname", "lname", "account" };
 String[] missing = parser.getMissingParameters(required);
 
The default charset for input parameters is ISO-8859-1 (Latin-1). If the parameter values are encoded in another format, specify that using setCharacterEncoding() before parsing. The parameter names currently have to be in the Latin-1 character set:
 ParameterParser parser = new ParameterParser(req);
 parser.setCharacterEncoding("Shift_JIS");
 String japaneseValue = parser.getStringParameter("latinName");
 

版本:
1.4, 2000/12/14, better checking the selected encoding is valid in setCharacterEncoding() thanks to Dewayne McNair, 1.3, 2000/05/17, added setCharacterEncoding(), 1.2, 2000/05/17, getBooleanParameter() now recognizes "on" and "yes", 1.1, 1999/12/20, added getMissingParameters() method, 1.0, 1998/09/18
作者:
Jason Hunter, Copyright © 1998, 1999
另请参见:
ParameterNotFoundException

构造方法摘要
ParameterParser(javax.servlet.ServletRequest req)
          Constructs a new ParameterParser to handle the parameters of the given request.
 
方法摘要
 boolean getBooleanParameter(String name)
          Gets the named parameter value as a boolean, with true indicated by "true", "on", or "yes" in any letter case, false indicated by "false", "off", or "no" in any letter case.
 boolean getBooleanParameter(String name, boolean def)
          Gets the named parameter value as a boolean, with a default.
 byte getByteParameter(String name)
          Gets the named parameter value as a byte
 byte getByteParameter(String name, byte def)
          Gets the named parameter value as a byte, with a default.
 char getCharParameter(String name)
          Gets the named parameter value as a char
 char getCharParameter(String name, char def)
          Gets the named parameter value as a char, with a default.
 double getDoubleParameter(String name)
          Gets the named parameter value as a double
 double getDoubleParameter(String name, double def)
          Gets the named parameter value as a double, with a default.
 float getFloatParameter(String name)
          Gets the named parameter value as a float
 float getFloatParameter(String name, float def)
          Gets the named parameter value as a float, with a default.
 int getIntParameter(String name)
          Gets the named parameter value as a int
 int getIntParameter(String name, int def)
          Gets the named parameter value as a int, with a default.
 long getLongParameter(String name)
          Gets the named parameter value as a long
 long getLongParameter(String name, long def)
          Gets the named parameter value as a long, with a default.
 String[] getMissingParameters(String[] required)
          Determines which of the required parameters were missing from the request.
 short getShortParameter(String name)
          Gets the named parameter value as a short
 short getShortParameter(String name, short def)
          Gets the named parameter value as a short, with a default.
 String getStringParameter(String name)
          Gets the named parameter value as a String
 String getStringParameter(String name, String def)
          Gets the named parameter value as a String, with a default.
 void setCharacterEncoding(String encoding)
          Sets the character encoding (charset) of the request to help the parser properly decode parameter values.
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

ParameterParser

public ParameterParser(javax.servlet.ServletRequest req)
Constructs a new ParameterParser to handle the parameters of the given request.

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

setCharacterEncoding

public void setCharacterEncoding(String encoding)
                          throws UnsupportedEncodingException
Sets the character encoding (charset) of the request to help the parser properly decode parameter values. The default is to return undecoded values, the same as would be returned by getParameter().

参数:
encoding - the charset of the request
抛出:
UnsupportedEncodingException - if the charset is not supported on this sytem

getStringParameter

public String getStringParameter(String name)
                          throws ParameterNotFoundException
Gets the named parameter value as a String

参数:
name - the parameter name
返回:
the parameter value as a String
抛出:
ParameterNotFoundException - if the parameter was not found or was the empty string

getStringParameter

public String getStringParameter(String name,
                                 String def)
Gets the named parameter value as a String, with a default. Returns the default value if the parameter is not found or is the empty string.

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

getBooleanParameter

public boolean getBooleanParameter(String name)
                            throws ParameterNotFoundException,
                                   NumberFormatException
Gets the named parameter value as a boolean, with true indicated by "true", "on", or "yes" in any letter case, false indicated by "false", "off", or "no" in any letter case.

参数:
name - the parameter name
返回:
the parameter value as a boolean
抛出:
ParameterNotFoundException - if the parameter was not found
NumberFormatException - if the parameter could not be converted to a boolean

getBooleanParameter

public boolean getBooleanParameter(String name,
                                   boolean def)
Gets the named parameter value as a boolean, with a default. Returns the default value if the parameter is not found.

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

getByteParameter

public byte getByteParameter(String name)
                      throws ParameterNotFoundException,
                             NumberFormatException
Gets the named parameter value as a byte

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

getByteParameter

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

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

getCharParameter

public char getCharParameter(String name)
                      throws ParameterNotFoundException
Gets the named parameter value as a char

参数:
name - the parameter name
返回:
the parameter value as a char
抛出:
ParameterNotFoundException - if the parameter was not found or was the empty string

getCharParameter

public char getCharParameter(String name,
                             char def)
Gets the named parameter value as a char, with a default. Returns the default value if the parameter is not found.

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

getDoubleParameter

public double getDoubleParameter(String name)
                          throws ParameterNotFoundException,
                                 NumberFormatException
Gets the named parameter value as a double

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

getDoubleParameter

public double getDoubleParameter(String name,
                                 double def)
Gets the named parameter value as a double, with a default. Returns the default value if the parameter is not found.

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

getFloatParameter

public float getFloatParameter(String name)
                        throws ParameterNotFoundException,
                               NumberFormatException
Gets the named parameter value as a float

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

getFloatParameter

public float getFloatParameter(String name,
                               float def)
Gets the named parameter value as a float, with a default. Returns the default value if the parameter is not found.

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

getIntParameter

public int getIntParameter(String name)
                    throws ParameterNotFoundException,
                           NumberFormatException
Gets the named parameter value as a int

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

getIntParameter

public int getIntParameter(String name,
                           int def)
Gets the named parameter value as a int, with a default. Returns the default value if the parameter is not found.

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

getLongParameter

public long getLongParameter(String name)
                      throws ParameterNotFoundException,
                             NumberFormatException
Gets the named parameter value as a long

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

getLongParameter

public long getLongParameter(String name,
                             long def)
Gets the named parameter value as a long, with a default. Returns the default value if the parameter is not found.

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

getShortParameter

public short getShortParameter(String name)
                        throws ParameterNotFoundException,
                               NumberFormatException
Gets the named parameter value as a short

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

getShortParameter

public short getShortParameter(String name,
                               short def)
Gets the named parameter value as a short, with a default. Returns the default value if the parameter is not found.

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

getMissingParameters

public String[] getMissingParameters(String[] required)
Determines which of the required parameters were missing from the request. Returns null if all the parameters are present.

参数:
required - array of required parameters
返回:
an array of missing parameters, or null if none are missing


Copyright © 2013. All Rights Reserved.