public class ParameterParser extends 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
Modifier and Type | Field and Description |
---|---|
private String |
encoding |
private ServletRequest |
req |
Constructor and Description |
---|
ParameterParser(ServletRequest req)
Constructs a new ParameterParser to handle the parameters of the
given request.
|
Modifier and Type | Method and Description |
---|---|
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.
|
private ServletRequest req
public ParameterParser(ServletRequest req)
req
- the servlet requestpublic void setCharacterEncoding(String encoding) throws UnsupportedEncodingException
encoding
- the charset of the requestUnsupportedEncodingException
- if the charset is not supported
on this sytempublic String getStringParameter(String name) throws ParameterNotFoundException
name
- the parameter nameParameterNotFoundException
- if the parameter was not found
or was the empty stringpublic String getStringParameter(String name, String def)
name
- the parameter namedef
- the default parameter valuepublic boolean getBooleanParameter(String name) throws ParameterNotFoundException, NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundNumberFormatException
- if the parameter could not be converted
to a booleanpublic boolean getBooleanParameter(String name, boolean def)
name
- the parameter namedef
- the default parameter valuepublic byte getByteParameter(String name) throws ParameterNotFoundException, NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundNumberFormatException
- if the parameter value could not
be converted to a bytepublic byte getByteParameter(String name, byte def)
name
- the parameter namedef
- the default parameter valuepublic char getCharParameter(String name) throws ParameterNotFoundException
name
- the parameter nameParameterNotFoundException
- if the parameter was not found
or was the empty stringpublic char getCharParameter(String name, char def)
name
- the parameter namedef
- the default parameter valuepublic double getDoubleParameter(String name) throws ParameterNotFoundException, NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundNumberFormatException
- if the parameter could not be converted
to a doublepublic double getDoubleParameter(String name, double def)
name
- the parameter namedef
- the default parameter valuepublic float getFloatParameter(String name) throws ParameterNotFoundException, NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundNumberFormatException
- if the parameter could not be converted
to a floatpublic float getFloatParameter(String name, float def)
name
- the parameter namedef
- the default parameter valuepublic int getIntParameter(String name) throws ParameterNotFoundException, NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundNumberFormatException
- if the parameter could not be converted
to a intpublic int getIntParameter(String name, int def)
name
- the parameter namedef
- the default parameter valuepublic long getLongParameter(String name) throws ParameterNotFoundException, NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundNumberFormatException
- if the parameter could not be converted
to a longpublic long getLongParameter(String name, long def)
name
- the parameter namedef
- the default parameter valuepublic short getShortParameter(String name) throws ParameterNotFoundException, NumberFormatException
name
- the parameter nameParameterNotFoundException
- if the parameter was not foundNumberFormatException
- if the parameter could not be converted
to a shortpublic short getShortParameter(String name, short def)
name
- the parameter namedef
- the default parameter valuepublic String[] getMissingParameters(String[] required)
an
- array of required parametersWebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses