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页面中没有现实冲突,而且代码实际上并没有任何冲突. 后来在这篇 ...
随机推荐
- EF有外键的查询
modelBuilder.Entity<ActionMenu>().ToTable("ActionMenu"); modelBuilder.Entity<Acti ...
- Python 购物车---之商家部分
知识点:文件写入操作, 函数, 函数递归 #!C:\Program Files\Python35/bin # -*- conding:utf-8 -*- # author: Frank # 定义商品列 ...
- Redis使用(1)---安装
简介 Redis具体简介可以百度.它可以在Linux和Windows下运行,但官方只提供针对Linux的包,Windows对其也有自己的开发团队. redis官网:http://www.redis.i ...
- 关于viewWithTag的一点说明
通常我们使用viewWithTag如下情形: 如果我们用了一个父View,上面放了多个子view, 每个子view都通过从0开始的Tag值来进行标志,以便于后期在像View上直接使用viewWithT ...
- scons构建自己的一个简单的程序
我在我的D盘下,新建一个文件夹,命名为try.在这个文件夹下新建两个文件,一个文件是test.c .里面的程序很简单: #include<stdio.h>#include<stdli ...
- 自定义Filter服务
自定义一个用户Email长度超过12个字符后值截取前12个然后添加“...”显示. 例如: index.html <!DOCTYPE html> <html ng-app=" ...
- 开发反模式(GUID) - 伪键洁癖
一.目标:整理数据 有的人有强迫症,他们会为一系列数据的断档而抓狂. 一方面,Id为3这一行确实发生过一些事情,为什么这个查询不返回Id为3的这一行?这条记录数据丢失了吗?那个Column到底是什么? ...
- shell操作mysql
参考: http://blog.csdn.net/hbcui1984/article/details/5125387
- REST API初识及设计
网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...
- Linux下高效数据恢复软件extundelete应用实战
作为一名运维人员,保证数据的安全是根本职责,所以在维护系统的时候,要慎之又慎,但是有时难免会出现数据被误删除的情况,在这个时候改如何快速.有效地恢复数据呢?本文我们就来介绍一下Linux系统下常用的几 ...