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. 深入理解Azure自动扩展集VMSS(1)

    前文中已经详细介绍了如何配置和部署Azure的虚拟机扩展集VMSS进行自动扩展,但在实际使用过程当中,用户会出现更进一步使用的一些问题,VMSS基本扩展原理及怎么简单调试?如何进行手动扩展?怎么使用自 ...

  2. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  3. 这样就算会了PHP么?-10

    关于基本的文件读写内容: <?php echo "readfile function:<br>"; readfile("tm.txt"); e ...

  4. Ceph:一个开源的 Linux PB 级分布式文件系统

    探索 Ceph 文件系统和生态系统 M. Tim Jones , 自由作家 简介:  Linux®持续不断进军可扩展计算空间,特别是可扩展存储空间.Ceph 最近才加入到 Linux 中令人印象深刻的 ...

  5. < welcome > 一起学习,进步,分享。

    现在时间:2014-3-24 hello world my blog. 第一次做博客,欢迎各路朋友指教.慢慢的分享学习到得东西,本人目前正在做IOS,也在学习数据库简单地应用. 作为一个IOS开发者, ...

  6. linux下tcpdump命令详解

    简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...

  7. Java 自定义实现 LRU 缓存算法

    背景 LinkedHashMap继承自HashMap,内部提供了一个removeEldestEntry方法,该方法正是实现LRU策略的关键所在,且HashMap内部专门为LinkedHashMap提供 ...

  8. [转载]Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结

    转载:http://blog.csdn.net/ithomer/article/details/6100734 知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式 ...

  9. UITextView 输入长度限制

    //还可以输入的长度. - (void)textViewDidChange:(UITextView *)textView { UITextRange *markRange = textView.mar ...

  10. The Frog's Games(二分)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...