设为首页 收藏本站
查看: 388|回复: 0

[经验分享] tomcat request.getParameter() 研究,api解读,以及解码解释

[复制链接]

尚未签到

发表于 2017-2-6 12:12:33 | 显示全部楼层 |阅读模式
  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,会影响这个方法的执行

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

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

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

该方法又调用了 org.apache.catalina.core.ApplicationHttpRequest.mergeParameters()来merge 参数
我们来读下它的源码
 
 
 



/**
* 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;
}

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

这就是 为什么过滤器 ,过滤中文,会使用 request.setCharacterEncoding(CharsetType.UTF8); 的原因
如果不设置值,默认 使用ISO-8859-1
 
上面就是 很多网站,很多攻略  所说的 request.getParameter()会内部对参数解码 的实现过程

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-338371-1-1.html 上篇帖子: 解析Tomcat处理请求的类Connector<二> 下篇帖子: Tomcat限制用域名访问,拒绝ip访问
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表