码迷,mamicode.com
首页 > 其他好文 > 详细

request.getParamer()

时间:2017-01-09 23:47:30      阅读:488      评论:0      收藏:0      [点我收藏+]

标签:中文   direct   影响   read   读取   format   res   格式   ret   

eturns 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,会影响这个方法的执行

 

网站上,经常有人说 这个方法 会先对参数进行解码,学的时候不求甚解,现在我们来看看到底是怎么回事:

 

就tomcat来说

 

具体实现 源码 是org.apache.catalina.core.ApplicationHttpRequest.getParameter(String)

 

它调用了 专门的 org.apache.catalina.core.ApplicationHttpRequest.parseParameters()方法来转换参数

 

该方法又调用了 org.apache.catalina.core.ApplicationHttpRequest.mergeParameters()来merge 参数

 

我们来读下它的源码

 

 

 

 

Java代码  技术分享
  1. /** 
  2.  * Merge the parameters from the saved query parameter string (if any), <br> 
  3.  * and the parameters already present on this request (if any), <br> 
  4.  * such that the parameter values from the query string show up first if there are duplicate parameter names. 
  5.  */  
  6. private void mergeParameters(){  
  7.     if ((queryParamString == null) || (queryParamString.length() < 1))  
  8.         return;  
  9.     HashMap queryParameters = new HashMap();  
  10.     String encoding = getCharacterEncoding();  
  11.     if (encoding == null)  
  12.         encoding = "ISO-8859-1";  
  13.     try{  
  14.         RequestUtil.parseParameters(queryParameters, queryParamString, encoding);  
  15.     }catch (Exception e){  
  16.         ;  
  17.     }  
  18.     Iterator keys = parameters.keySet().iterator();  
  19.     while (keys.hasNext()){  
  20.         String key = (String) keys.next();  
  21.         Object value = queryParameters.get(key);  
  22.         if (value == null){  
  23.             queryParameters.put(key, parameters.get(key));  
  24.             continue;  
  25.         }  
  26.         queryParameters.put(key, mergeValues(value, parameters.get(key)));  
  27.     }  
  28.     parameters = queryParameters;  
  29. }  

 

 

可以看到 ,先会使用 encoding 对参数进行编码 ,这里调用了 getCharacterEncoding()

这就是 为什么过滤器 ,过滤中文,会使用request.setCharacterEncoding(CharsetType.UTF8); 的原因

 

如果不设置值,默认 使用ISO-8859-1

 

上面就是 很多网站,很多攻略  所说的 request.getParameter()会内部对参数解码 的实现过程

request.getParamer()

标签:中文   direct   影响   read   读取   format   res   格式   ret   

原文地址:http://www.cnblogs.com/ziq711/p/6266831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!