|
||||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectcom.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:
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");
ParameterNotFoundException| 构造方法摘要 | |
|---|---|
ParameterParser(javax.servlet.ServletRequest req)
Constructs a new ParameterParser to handle the parameters of the given request. |
|
| 方法摘要 | |
|---|---|
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. |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| 构造方法详细信息 |
|---|
public ParameterParser(javax.servlet.ServletRequest req)
req - the servlet request| 方法详细信息 |
|---|
public void setCharacterEncoding(String encoding)
throws UnsupportedEncodingException
encoding - the charset of the request
UnsupportedEncodingException - if the charset is not supported
on this sytem
public String getStringParameter(String name)
throws ParameterNotFoundException
name - the parameter name
ParameterNotFoundException - if the parameter was not found
or was the empty string
public String getStringParameter(String name,
String def)
name - the parameter namedef - the default parameter value
public boolean getBooleanParameter(String name)
throws ParameterNotFoundException,
NumberFormatException
name - the parameter name
ParameterNotFoundException - if the parameter was not found
NumberFormatException - if the parameter could not be converted
to a boolean
public boolean getBooleanParameter(String name,
boolean def)
name - the parameter namedef - the default parameter value
public byte getByteParameter(String name)
throws ParameterNotFoundException,
NumberFormatException
name - the parameter name
ParameterNotFoundException - if the parameter was not found
NumberFormatException - if the parameter value could not
be converted to a byte
public byte getByteParameter(String name,
byte def)
name - the parameter namedef - the default parameter value
public char getCharParameter(String name)
throws ParameterNotFoundException
name - the parameter name
ParameterNotFoundException - if the parameter was not found
or was the empty string
public char getCharParameter(String name,
char def)
name - the parameter namedef - the default parameter value
public double getDoubleParameter(String name)
throws ParameterNotFoundException,
NumberFormatException
name - the parameter name
ParameterNotFoundException - if the parameter was not found
NumberFormatException - if the parameter could not be converted
to a double
public double getDoubleParameter(String name,
double def)
name - the parameter namedef - the default parameter value
public float getFloatParameter(String name)
throws ParameterNotFoundException,
NumberFormatException
name - the parameter name
ParameterNotFoundException - if the parameter was not found
NumberFormatException - if the parameter could not be converted
to a float
public float getFloatParameter(String name,
float def)
name - the parameter namedef - the default parameter value
public int getIntParameter(String name)
throws ParameterNotFoundException,
NumberFormatException
name - the parameter name
ParameterNotFoundException - if the parameter was not found
NumberFormatException - if the parameter could not be converted
to a int
public int getIntParameter(String name,
int def)
name - the parameter namedef - the default parameter value
public long getLongParameter(String name)
throws ParameterNotFoundException,
NumberFormatException
name - the parameter name
ParameterNotFoundException - if the parameter was not found
NumberFormatException - if the parameter could not be converted
to a long
public long getLongParameter(String name,
long def)
name - the parameter namedef - the default parameter value
public short getShortParameter(String name)
throws ParameterNotFoundException,
NumberFormatException
name - the parameter name
ParameterNotFoundException - if the parameter was not found
NumberFormatException - if the parameter could not be converted
to a short
public short getShortParameter(String name,
short def)
name - the parameter namedef - the default parameter value
public String[] getMissingParameters(String[] required)
required - array of required parameters
|
||||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||