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 参数

我们来读下它的源码

  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()的更多相关文章

  1. 前端面试题第二波,要offer的看过来~

    快来测试测试自己掌握能力吧! 1. class.forname的作用?为什么要用? 1).获取Class对象的方式:类名.class.对象.getClass().Class.forName(" ...

  2. web前端面试第三波~

    快来测试测试自己掌握能力吧! 1. class.forname的作用?为什么要用? 1).获取Class对象的方式:类名.class.对象.getClass().Class.forName(" ...

  3. spring Mvc Web 编码相关 [model 到 视图传递数据] (九)

    在某种编码环境,由bean注解的参数可能会发生乱码问题. 即可页面web.xml或其他地方都设备UTF-8, 但还是会有这样的问题. 首先不要使用model传到视图的数据. 第二,不要request. ...

  4. java web 程序---登陆验证4个页面

    思路: 1.第一个是登陆页面login.jsp一个form表单.点击登陆按钮 2.第二个是验证页面check.jsp.如果username和password都正确.则跳转到另一个页面a.jsp显示登陆 ...

  5. java web学习笔记-jsp篇

    1.java web简介 1.1静态页面与动态页面   表现形式 所需技术 静态网页 网页内容固定,不会更新 html,css 动态网页 网页内容由程序动态显示,自动更新 html,css,DB,ja ...

  6. Java-日期格式转换

    1.日期-String类型转Date类型 // String转Date str = "2007-1-18"; DateFormat format1 = new SimpleDate ...

  7. JSP内置对象--request对象

    本文主要介绍JSP中的request对象 request对象的主要方法: setAttribute(String name,Object):设置名字为name的request的参数值 getAttri ...

  8. Concepts:Request 和 Task

    当SQL Server Engine 接收到Session发出的Request时,SQL Server OS将Request和Task绑定,并为Task分配一个Workder.在TSQL Query执 ...

  9. 解决托管在Windows上的Stash的Pull request无法合并的问题

    最近尝试合并一个托管在Windows的Stash系统中的pull request时,发现合并按钮被禁用,显示有冲突不能合并,但是在diff页面中没有现实冲突,而且代码实际上并没有任何冲突. 后来在这篇 ...

随机推荐

  1. 图铭Android平台银行卡号识别系统

    随着智能终端(智能手机及平板电脑)及移动通信(3G)的发展,原来运行在PC上的信息系统(如邮件系统.即时通信.网页浏览.协同办公.网络购物.社交网站.博客等)逐渐转移到智能终端设备上.可以预见未来几年 ...

  2. VirtualBox虚拟机无法选择桥接方式

    VirtualBox 装好之后默认的网络是NAT模式,但这种模式中虚拟机配置的IP和主机的不再同一网段内,无法获得和主机一样的局域网地位,更不可能从主机用远程桌面携带硬盘的方式远程控制.而最简便的方法 ...

  3. 非索引列上的统计 <第二篇>

    非索引列上的统计 有时候,可能在连接或过滤条件中的列上没有索引.即使对这种非索引列,如果查询优化器知道这些列的数据分布(统计),它也很可能做出最佳的选择. 除了索引上的统计,SQL Server可以在 ...

  4. Spring使用小结2

    之前做过不少spring想过知识点内容的摘录, Spring框架的特点.模块组成.优缺点 spring相关的bean管理想过知识点及依赖注入方式 今天说下近端时间中项目中遇到的相关印象比较深的知识点 ...

  5. 关于Store Apps

    因为时代在变迁,Store Apps这个概念很容易引起混淆 在过去,windows phone 8.0时代 windows store apps指的是windows metro style 的应用, ...

  6. 方案:手动升级WordPress系统

    对于WordPress系统及时进行更新维护是十分必须的操作,更新维护不仅可以更新系统服务功能,还能够完善安全系统.      如果你是虚拟主机的用户,可以使用FTP账户进行自动更新服务,但是如果你是V ...

  7. wpf纯前台绑定

    <Window x:Class="Example1.MainWindow" ... xmlns:local="clr-namespace:Example1" ...

  8. <% %> 、 <%= %> 、<%# %> 的区别

    1,<% %>用来绑定后台代码    中间一般放函数或者方法,典型的asp程序写法. 在前台页面可以写后台代码                 相当于开辟了C#空间,可以写C#代码 2,& ...

  9. Linux查看系统信息

    系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...

  10. go中string和slice no-copy转换

    在go里面,string和slice的互换是需要进行内存拷贝的,虽然在底层,它们都只是用 pointer + len来表示的一段内存. 通常,我们不会在意string和slice的转换带来的内存拷贝性 ...