request.getParamer()
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 参数
我们来读下它的源码
- /**
- * 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()会内部对参数解码 的实现过程
request.getParamer()的更多相关文章
- 前端面试题第二波,要offer的看过来~
快来测试测试自己掌握能力吧! 1. class.forname的作用?为什么要用? 1).获取Class对象的方式:类名.class.对象.getClass().Class.forName(" ...
- web前端面试第三波~
快来测试测试自己掌握能力吧! 1. class.forname的作用?为什么要用? 1).获取Class对象的方式:类名.class.对象.getClass().Class.forName(" ...
- spring Mvc Web 编码相关 [model 到 视图传递数据] (九)
在某种编码环境,由bean注解的参数可能会发生乱码问题. 即可页面web.xml或其他地方都设备UTF-8, 但还是会有这样的问题. 首先不要使用model传到视图的数据. 第二,不要request. ...
- java web 程序---登陆验证4个页面
思路: 1.第一个是登陆页面login.jsp一个form表单.点击登陆按钮 2.第二个是验证页面check.jsp.如果username和password都正确.则跳转到另一个页面a.jsp显示登陆 ...
- java web学习笔记-jsp篇
1.java web简介 1.1静态页面与动态页面 表现形式 所需技术 静态网页 网页内容固定,不会更新 html,css 动态网页 网页内容由程序动态显示,自动更新 html,css,DB,ja ...
- Java-日期格式转换
1.日期-String类型转Date类型 // String转Date str = "2007-1-18"; DateFormat format1 = new SimpleDate ...
- JSP内置对象--request对象
本文主要介绍JSP中的request对象 request对象的主要方法: setAttribute(String name,Object):设置名字为name的request的参数值 getAttri ...
- Concepts:Request 和 Task
当SQL Server Engine 接收到Session发出的Request时,SQL Server OS将Request和Task绑定,并为Task分配一个Workder.在TSQL Query执 ...
- 解决托管在Windows上的Stash的Pull request无法合并的问题
最近尝试合并一个托管在Windows的Stash系统中的pull request时,发现合并按钮被禁用,显示有冲突不能合并,但是在diff页面中没有现实冲突,而且代码实际上并没有任何冲突. 后来在这篇 ...
随机推荐
- CS找工作好文章
我的美国CS面试经验分享 -- 转载 怎样花两年时间去面试一个人 上面列出了一些比较好的书单 cs土硕找工作总结(二) 笔试面试准备http://blog.renren.com/blog/221227 ...
- java设计模式--行为型模式--模板方法
什么是模板方法,这个有待考虑,看下面: 模板方法 概述 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步 ...
- 单目录下多文件 makefile编写
makefile很久就接触过了,但是一直没怎么深入的去学习和总结:在项目中我也只是看看makefile或者修改部分语句,全部自己动手写的话还真没有:知识在于沉淀,这句说的非常好,所以现在把自己理解的东 ...
- POJ-2533最长上升子序列(DP+二分)(优化版)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41944 Acc ...
- 【Cocos2d-X游戏实战开发】捕鱼达人之游戏场景的创建(六)
本系列学习教程使用的是cocos2d-x-2.1.4(最新版为cocos2d-x-2.1.5) 博主发现前两个系列的学习教程被严重抄袭,在这里呼吁大家请尊重开发者的劳动成果, 转载的时候请务必注 ...
- UILabel+Create
#import <UIKit/UIKit.h> @interface UILabel (Create) /** * 创建普通Label * * @param frame frame * @ ...
- 格而知之15:我所理解的Block(1)
1.Block 本质上是一个struct结构体,在这个结构体中,最重要的成员是一个函数(当然除函数外还有其他重要的成员). 2.在开始解析Block之前,首先来回顾一下Block的格式.Block相关 ...
- C#操作IE
操作IE主要使用两个Com Dll: 1.Microsoft Internet Controls 2.Microsoft HTML Object Library 使用Microsoft Interne ...
- 多线程下不反复读取SQL Server 表的数据
在进行一些如发送短信.邮件的业务时,我们常常会使用一个表来存储待发送的数据,由后台多个线程不断的从表中读取待发送的数据进行发送.发送完毕后再将数据转移到历史表中,这样保证待发送表的数据普通情况下不会太 ...
- Hash表的扩容(转载)
Hash表(Hash Table) hash表实际上由size个的桶组成一个桶数组table[0...size-1] . 当一个对象经过哈希之后.得到一个对应的value , 于是我们把这个对象放 ...