public class ParameterParser
extends java.lang.Object
It is used like this:
There's also a capability to find out if any required parameters are missing from a request: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(); }
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); String[] required = { "fname", "lname", "account" }; String[] missing = parser.getMissingParameters(required);
ParameterParser parser = new ParameterParser(req); parser.setCharacterEncoding("Shift_JIS"); String japaneseValue = parser.getStringParameter("latinName");
ParameterNotFoundException
Constructor and Description |
---|
ParameterParser(javax.servlet.ServletRequest req)
Constructs a new ParameterParser to handle the parameters of the
given request.
|
Modifier and Type | Method and Description |
---|---|
boolean |
getBooleanParameter(java.lang.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(java.lang.String name,
boolean def)
Gets the named parameter value as a boolean, with a default.
|
byte |
getByteParameter(java.lang.String name)
Gets the named parameter value as a byte
|
byte |
getByteParameter(java.lang.String name,
byte def)
Gets the named parameter value as a byte, with a default.
|
char |
getCharParameter(java.lang.String name)
Gets the named parameter value as a char
|
char |
getCharParameter(java.lang.String name,
char def)
Gets the named parameter value as a char, with a default.
|
double |
getDoubleParameter(java.lang.String name)
Gets the named parameter value as a double
|
double |
getDoubleParameter(java.lang.String name,
double def)
Gets the named parameter value as a double, with a default.
|
float |
getFloatParameter(java.lang.String name)
Gets the named parameter value as a float
|
float |
getFloatParameter(java.lang.String name,
float def)
Gets the named parameter value as a float, with a default.
|
int |
getIntParameter(java.lang.String name)
Gets the named parameter value as a int
|
int |
getIntParameter(java.lang.String name,
int def)
Gets the named parameter value as a int, with a default.
|
long |
getLongParameter(java.lang.String name)
Gets the named parameter value as a long
|
long |
getLongParameter(java.lang.String name,
long def)
Gets the named parameter value as a long, with a default.
|
java.lang.String[] |
getMissingParameters(java.lang.String[] required)
Determines which of the required parameters were missing from the
request.
|
short |
getShortParameter(java.lang.String name)
Gets the named parameter value as a short
|
short |
getShortParameter(java.lang.String name,
short def)
Gets the named parameter value as a short, with a default.
|
java.lang.String |
getStringParameter(java.lang.String name)
Gets the named parameter value as a String
|
java.lang.String |
getStringParameter(java.lang.String name,
java.lang.String def)
Gets the named parameter value as a String, with a default.
|
void |
setCharacterEncoding(java.lang.String encoding)
Sets the character encoding (charset) of the request to help the parser
properly decode parameter values.
|
public ParameterParser(javax.servlet.ServletRequest req)
req
- the servlet requestpublic void setCharacterEncoding(java.lang.String encoding) throws java.io.UnsupportedEncodingException
encoding
- the charset of the requestjava.io.UnsupportedEncodingException
- if the charset is not supported
on this sytempublic java.lang.String getStringParameter(java.lang.String name) throws ParameterNotFoundException
name
- the parameter nameParameterNotFoundException
- if the parameter was not found
or was the empty stringpublic java.lang.String getStringParameter(java.lang.String name, java.lang.String def)
name
- the parameter namedef
- the default parameter valuepublic boolean getBooleanParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundjava.lang.NumberFormatException
- if the parameter could not be converted
to a booleanpublic boolean getBooleanParameter(java.lang.String name, boolean def)
name
- the parameter namedef
- the default parameter valuepublic byte getByteParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundjava.lang.NumberFormatException
- if the parameter value could not
be converted to a bytepublic byte getByteParameter(java.lang.String name, byte def)
name
- the parameter namedef
- the default parameter valuepublic char getCharParameter(java.lang.String name) throws ParameterNotFoundException
name
- the parameter nameParameterNotFoundException
- if the parameter was not found
or was the empty stringpublic char getCharParameter(java.lang.String name, char def)
name
- the parameter namedef
- the default parameter valuepublic double getDoubleParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundjava.lang.NumberFormatException
- if the parameter could not be converted
to a doublepublic double getDoubleParameter(java.lang.String name, double def)
name
- the parameter namedef
- the default parameter valuepublic float getFloatParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundjava.lang.NumberFormatException
- if the parameter could not be converted
to a floatpublic float getFloatParameter(java.lang.String name, float def)
name
- the parameter namedef
- the default parameter valuepublic int getIntParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundjava.lang.NumberFormatException
- if the parameter could not be converted
to a intpublic int getIntParameter(java.lang.String name, int def)
name
- the parameter namedef
- the default parameter valuepublic long getLongParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundjava.lang.NumberFormatException
- if the parameter could not be converted
to a longpublic long getLongParameter(java.lang.String name, long def)
name
- the parameter namedef
- the default parameter valuepublic short getShortParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundjava.lang.NumberFormatException
- if the parameter could not be converted
to a shortpublic short getShortParameter(java.lang.String name, short def)
name
- the parameter namedef
- the default parameter valuepublic java.lang.String[] getMissingParameters(java.lang.String[] required)
required
- array of required parameters