|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.oreilly.servlet.ParameterParser
public class ParameterParser
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:
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 Summary | |
---|---|
ParameterParser(ServletRequest req)
Constructs a new ParameterParser to handle the parameters of the given request. |
Method Summary | |
---|---|
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. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ParameterParser(ServletRequest req)
req
- the servlet requestMethod Detail |
---|
public void setCharacterEncoding(java.lang.String encoding) throws java.io.UnsupportedEncodingException
encoding
- the charset of the request
java.io.UnsupportedEncodingException
- if the charset is not supported
on this sytempublic java.lang.String getStringParameter(java.lang.String name) throws ParameterNotFoundException
name
- the parameter name
ParameterNotFoundException
- 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 value
public boolean getBooleanParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter name
ParameterNotFoundException
- if the parameter was not found
java.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 value
public byte getByteParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter name
ParameterNotFoundException
- if the parameter was not found
java.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 value
public char getCharParameter(java.lang.String name) throws ParameterNotFoundException
name
- the parameter name
ParameterNotFoundException
- 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 value
public double getDoubleParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter name
ParameterNotFoundException
- if the parameter was not found
java.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 value
public float getFloatParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter name
ParameterNotFoundException
- if the parameter was not found
java.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 value
public int getIntParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter name
ParameterNotFoundException
- if the parameter was not found
java.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 value
public long getLongParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter name
ParameterNotFoundException
- if the parameter was not found
java.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 value
public short getShortParameter(java.lang.String name) throws ParameterNotFoundException, java.lang.NumberFormatException
name
- the parameter name
ParameterNotFoundException
- if the parameter was not found
java.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 value
public java.lang.String[] getMissingParameters(java.lang.String[] required)
an
- array of required parameters
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |