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

我们来读下它的源码

Java代码  

  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()会内部对参数解码 的实现过程

时间: 2024-08-05 17:28:51

request.getParamer()的相关文章

java web学习笔记-jsp篇

转载自:http://www.cnblogs.com/happyfans/archive/2015/03/17/4343571.html 1.java web简介 1.1静态页面与动态页面   表现形式 所需技术 静态网页 网页内容固定,不会更新 html,css 动态网页 网页内容由程序动态显示,自动更新 html,css,DB,java/c#/php,javascript,xml,主流的动态网页脚本(jsp,asp.net,php) 1.2搭建java web开发环境 jdk1.7+tomc

黑马day05 session&amp;重新设置JSESSIONID的生命周期

HttpSession:在服务器中,为浏览器创建独一无二的内存空间,在其中保存会话相关的信息. 4.1session作为域使用:他是j2ee中四大域对象之一,作用范围为整个会话. 4.2session的生命周期:在第一次调用reqeust.getSession()方法的时候,服务器会检查是已经有对应的session,如果没有就在内存中创建一个session并返回. 当一段时间内session没有被使用,一般为30分钟(此值可以在web.xml中配置<session-config>来配置,也可以

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

在某种编码环境,由bean注解的参数可能会发生乱码问题. 即可页面web.xml或其他地方都设备UTF-8, 但还是会有这样的问题. 首先不要使用model传到视图的数据. 第二,不要request.getParamer或request.getAttribute方式,这其他就有解码内置. 第三,首先对传进来的url,其中有汉字的进行escape加码,再传到目标端. 第四,经过escape的参数,使用request.getParameter或attribute获取值为 null spring Mv

1javascript语法

javascript是Navigator2.0中的关键组件之一,javascript是解释型的,以对象为基础的,易于使用与学习且紧紧集成与html的语言.javascript将当前浏览器会话的属性提供给了编程者,这些属性包括当前加载的html页面的元素,如窗体.框架以及链接.javascript是区分大小写. java与javascript之间存在着差别,虽然它们是相关的,javascript借用了java大多数的语法,但他们在本质上是不同的而且服务于不同的目的,可以说java与javascri

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

思路: 1.第一个是登陆页面login.jsp一个form表单.点击登陆按钮 2.第二个是验证页面check.jsp.如果username和password都正确.则跳转到另一个页面a.jsp显示登陆成功.否则,登陆有误,提醒用户,重新登陆. 3.当用户没有输入用户名和密码就直接进入登陆页面,那么有一个b.jsp页面,进行验证,用户名和密码都不为空,如果为空,则提示用户请先登陆. 其实,到这里,这么人性化的设计还是他,不是他,我他妈都不用这么烦,这么爱, login.jsp ? 1 2 3 4

JSP内置对象--request对象

本文主要介绍JSP中的request对象 request对象的主要方法: setAttribute(String name,Object):设置名字为name的request的参数值 getAttribute(String name):返回由name指定的属性值 getAttributeNames():返回request对象所有属性的名字集合,结果是一个枚举的实例 getCookies():返回客户端的所有Cookie对象,结果是一个Cookie数组 getCharacterEncoding()

HTTP 400 错误 - 请求无效 (Bad request)

在ajax请求后台数据时有时会报 HTTP 400 错误 - 请求无效 (Bad request);出现这个请求无效报错说明请求没有进入到后台服务里: 原因:1)前端提交数据的字段名称或者是字段类型和后台的实体类不一致,导致无法封装: 2)前端提交的到后台的数据应该是json字符串类型,而前端没有将对象转化为字符串类型: 解决方案: 1)对照字段名称,类型保证一致性 2)使用stringify将前端传递的对象转化为字符串    data: JSON.stringify(param)  ;

Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]56528192: startup date [Tue Sep 19 15:05:24 CST 2017]; root of context hierarchy 2017-09-19 15:05:24.858 INFO 9986 --

ASP.NET取得Request URL的各个部分

ASP.NET取得Request URL的各个部分  网址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc Request.ApplicationPath / Request.PhysicalPath D:\Projects\Solution\web\News\Press\Content.aspx System.IO.Path.GetDirectoryName(Request.PhysicalPath) D:\Projects