All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class 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");
 

Version:
1.4, 2000/12/14, better checking the selected encoding is valid in setCharacterEncoding() thanks to Dewayne McNair
Author:
Jason Hunter, Copyright © 1998, 1999
See Also:
ParameterNotFoundException

Constructor Index

 o ParameterParser(ServletRequest)
Constructs a new ParameterParser to handle the parameters of the given request.

Method Index

 o getBooleanParameter(String)
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.
 o getBooleanParameter(String, boolean)
Gets the named parameter value as a boolean, with a default.
 o getByteParameter(String)
Gets the named parameter value as a byte
 o getByteParameter(String, byte)
Gets the named parameter value as a byte, with a default.
 o getCharParameter(String)
Gets the named parameter value as a char
 o getCharParameter(String, char)
Gets the named parameter value as a char, with a default.
 o getDoubleParameter(String)
Gets the named parameter value as a double
 o getDoubleParameter(String, double)
Gets the named parameter value as a double, with a default.
 o getFloatParameter(String)
Gets the named parameter value as a float
 o getFloatParameter(String, float)
Gets the named parameter value as a float, with a default.
 o getIntParameter(String)
Gets the named parameter value as a int
 o getIntParameter(String, int)
Gets the named parameter value as a int, with a default.
 o getLongParameter(String)
Gets the named parameter value as a long
 o getLongParameter(String, long)
Gets the named parameter value as a long, with a default.
 o getMissingParameters(String[])
Determines which of the required parameters were missing from the request.
 o getShortParameter(String)
Gets the named parameter value as a short
 o getShortParameter(String, short)
Gets the named parameter value as a short, with a default.
 o getStringParameter(String)
Gets the named parameter value as a String
 o getStringParameter(String, String)
Gets the named parameter value as a String, with a default.
 o setCharacterEncoding(String)
Sets the character encoding (charset) of the request to help the parser properly decode parameter values.

Constructors

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

Parameters:
req - the servlet request

Methods

 o 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().

Parameters:
encoding - the charset of the request
Throws: UnsupportedEncodingException
if the charset is not supported on this sytem
 o getStringParameter
 public String getStringParameter(String name) throws ParameterNotFoundException
Gets the named parameter value as a String

Parameters:
name - the parameter name
Returns:
the parameter value as a String
Throws: ParameterNotFoundException
if the parameter was not found or was the empty string
 o 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.

Parameters:
name - the parameter name
def - the default parameter value
Returns:
the parameter value as a String, or the default
 o 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.

Parameters:
name - the parameter name
Returns:
the parameter value as a boolean
Throws: ParameterNotFoundException
if the parameter was not found
Throws: NumberFormatException
if the parameter could not be converted to a boolean
 o 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.

Parameters:
name - the parameter name
def - the default parameter value
Returns:
the parameter value as a boolean, or the default
 o getByteParameter
 public byte getByteParameter(String name) throws ParameterNotFoundException, NumberFormatException
Gets the named parameter value as a byte

Parameters:
name - the parameter name
Returns:
the parameter value as a byte
Throws: ParameterNotFoundException
if the parameter was not found
Throws: NumberFormatException
if the parameter value could not be converted to a byte
 o 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.

Parameters:
name - the parameter name
def - the default parameter value
Returns:
the parameter value as a byte, or the default
 o getCharParameter
 public char getCharParameter(String name) throws ParameterNotFoundException
Gets the named parameter value as a char

Parameters:
name - the parameter name
Returns:
the parameter value as a char
Throws: ParameterNotFoundException
if the parameter was not found or was the empty string
 o 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.

Parameters:
name - the parameter name
def - the default parameter value
Returns:
the parameter value as a char, or the default
 o getDoubleParameter
 public double getDoubleParameter(String name) throws ParameterNotFoundException, NumberFormatException
Gets the named parameter value as a double

Parameters:
name - the parameter name
Returns:
the parameter value as a double
Throws: ParameterNotFoundException
if the parameter was not found
Throws: NumberFormatException
if the parameter could not be converted to a double
 o 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.

Parameters:
name - the parameter name
def - the default parameter value
Returns:
the parameter value as a double, or the default
 o getFloatParameter
 public float getFloatParameter(String name) throws ParameterNotFoundException, NumberFormatException
Gets the named parameter value as a float

Parameters:
name - the parameter name
Returns:
the parameter value as a float
Throws: ParameterNotFoundException
if the parameter was not found
Throws: NumberFormatException
if the parameter could not be converted to a float
 o 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.

Parameters:
name - the parameter name
def - the default parameter value
Returns:
the parameter value as a float, or the default
 o getIntParameter
 public int getIntParameter(String name) throws ParameterNotFoundException, NumberFormatException
Gets the named parameter value as a int

Parameters:
name - the parameter name
Returns:
the parameter value as a int
Throws: ParameterNotFoundException
if the parameter was not found
Throws: NumberFormatException
if the parameter could not be converted to a int
 o 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.

Parameters:
name - the parameter name
def - the default parameter value
Returns:
the parameter value as a int, or the default
 o getLongParameter
 public long getLongParameter(String name) throws ParameterNotFoundException, NumberFormatException
Gets the named parameter value as a long

Parameters:
name - the parameter name
Returns:
the parameter value as a long
Throws: ParameterNotFoundException
if the parameter was not found
Throws: NumberFormatException
if the parameter could not be converted to a long
 o 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.

Parameters:
name - the parameter name
def - the default parameter value
Returns:
the parameter value as a long, or the default
 o getShortParameter
 public short getShortParameter(String name) throws ParameterNotFoundException, NumberFormatException
Gets the named parameter value as a short

Parameters:
name - the parameter name
Returns:
the parameter value as a short
Throws: ParameterNotFoundException
if the parameter was not found
Throws: NumberFormatException
if the parameter could not be converted to a short
 o 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.

Parameters:
name - the parameter name
def - the default parameter value
Returns:
the parameter value as a short, or the default
 o 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.

Parameters:
an - array of required parameters
Returns:
an array of missing parameters, or null if none are missing

All Packages  Class Hierarchy  This Package  Previous  Next  Index