Returns
the value of a request parameter as a String, or null if the parameter does not
exist. Request parameters are extra information sent with the request. For HTTP
servlets, parameters are contained in the query string or posted form data.
You
should only use this method when you are sure the parameter has only one value.
If the parameter might have more than one value, use getParameterValues.
If you
use this method with a multivalued parameter, the value returned is equal to
the first value in the array returned by getParameterValues.
If the
parameter data was sent in the request body, such as occurs with an HTTP POST
request, then reading the body directly via getInputStream
or getReader
can interfere with the execution of this method.
在英文api文档里面,对这个方法进行了很详细的阐述,我翻译一下:
返回一个字符串格式的请求参数值,如果这个请求参数不存在,那么返回null,请求参数 是 一个请求的扩展信息
就http servlets,请求参数包含在querystring 或者post提交的数据里面
当你确定这个请求参数只有唯一值,那么你使用这个方法,如果有多个值的话,你要使用getParameterValues.了,
如果你使用这个方法解析多值参数,那么将会返回getParameterValues结果的第一个值
如果参数数据经过request
body 传来的,比如 http post 请求,直接经过getInputStream or getReader 读取body,会影响这个方法的执行
网站上,经常有人说 这个方法 会先对参数进行解码,学的时候不求甚解,现在我们来看看到底是怎么回事:
/**
* Merge the parameters from the saved query parameter string (if any), <br>
* and the parameters already present on this request (if any), <br>
* such that the parameter values from the query string show up first if there are duplicate parameter names.
*/
private void mergeParameters(){
if ((queryParamString == null) || (queryParamString.length() < 1))
return;
HashMap queryParameters = new HashMap();
String encoding = getCharacterEncoding();
if (encoding == null)
encoding = "ISO-8859-1";
try{
RequestUtil.parseParameters(queryParameters, queryParamString, encoding);
}catch (Exception e){
;
}
Iterator keys = parameters.keySet().iterator();
while (keys.hasNext()){
String key = (String) keys.next();
Object value = queryParameters.get(key);
if (value == null){
queryParameters.put(key, parameters.get(key));
continue;
}
queryParameters.put(key, mergeValues(value, parameters.get(key)));
}
parameters = queryParameters;
}