| java.lang.Object com.oreilly.servlet.ParameterParser
ParameterParser | public class ParameterParser (Code) | | 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");
See Also: com.oreilly.servlet.ParameterNotFoundException author: Jason Hunter, Copyright © 1998, 1999 version: 1.4, 2000/12/14, better checking the selected encoding is valid in version: setCharacterEncoding() thanks to Dewayne McNair version: 1.3, 2000/05/17, added setCharacterEncoding() version: 1.2, 2000/05/17, getBooleanParameter() now recognizes "on" and "yes" version: 1.1, 1999/12/20, added getMissingParameters() method version: 1.0, 1998/09/18 |
Method Summary | |
public 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. | public boolean | getBooleanParameter(String name, boolean def) Gets the named parameter value as a boolean, with a default. | public byte | getByteParameter(String name) | public byte | getByteParameter(String name, byte def) Gets the named parameter value as a byte, with a default. | public char | getCharParameter(String name) | public char | getCharParameter(String name, char def) Gets the named parameter value as a char, with a default. | public double | getDoubleParameter(String name) | public double | getDoubleParameter(String name, double def) Gets the named parameter value as a double, with a default. | public float | getFloatParameter(String name) | public float | getFloatParameter(String name, float def) Gets the named parameter value as a float, with a default. | public int | getIntParameter(String name) | public int | getIntParameter(String name, int def) Gets the named parameter value as a int, with a default. | public long | getLongParameter(String name) | public long | getLongParameter(String name, long def) Gets the named parameter value as a long, with a default. | public String[] | getMissingParameters(String[] required) Determines which of the required parameters were missing from the
request. | public short | getShortParameter(String name) | public short | getShortParameter(String name, short def) Gets the named parameter value as a short, with a default. | public String | getStringParameter(String name) | public String | getStringParameter(String name, String def) Gets the named parameter value as a String, with a default. | public void | setCharacterEncoding(String encoding) Sets the character encoding (charset) of the request to help the parser
properly decode parameter values. |
ParameterParser | public ParameterParser(ServletRequest req)(Code) | | Constructs a new ParameterParser to handle the parameters of the
given request.
Parameters: req - the servlet request |
getBooleanParameter | public boolean getBooleanParameter(String name) throws ParameterNotFoundException, NumberFormatException(Code) | | 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 the parameter value as a boolean exception: ParameterNotFoundException - if the parameter was not found exception: NumberFormatException - if the parameter could not be converted to a boolean |
getBooleanParameter | public boolean getBooleanParameter(String name, boolean def)(Code) | | 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 Parameters: def - the default parameter value the parameter value as a boolean, or the default |
getByteParameter | public byte getByteParameter(String name, byte def)(Code) | | 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 Parameters: def - the default parameter value the parameter value as a byte, or the default |
getCharParameter | public char getCharParameter(String name, char def)(Code) | | 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 Parameters: def - the default parameter value the parameter value as a char, or the default |
getDoubleParameter | public double getDoubleParameter(String name, double def)(Code) | | 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 Parameters: def - the default parameter value the parameter value as a double, or the default |
getFloatParameter | public float getFloatParameter(String name, float def)(Code) | | 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 Parameters: def - the default parameter value the parameter value as a float, or the default |
getIntParameter | public int getIntParameter(String name, int def)(Code) | | 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 Parameters: def - the default parameter value the parameter value as a int, or the default |
getLongParameter | public long getLongParameter(String name, long def)(Code) | | 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 Parameters: def - the default parameter value the parameter value as a long, or the default |
getMissingParameters | public String[] getMissingParameters(String[] required)(Code) | | 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 an array of missing parameters, or null if none are missing |
getShortParameter | public short getShortParameter(String name, short def)(Code) | | 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 Parameters: def - the default parameter value the parameter value as a short, or the default |
getStringParameter | public String getStringParameter(String name, String def)(Code) | | 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 Parameters: def - the default parameter value the parameter value as a String, or the default |
setCharacterEncoding | public void setCharacterEncoding(String encoding) throws UnsupportedEncodingException(Code) | | 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 exception: UnsupportedEncodingException - if the charset is not supported on this sytem |
|
|