zoukankan      html  css  js  c++  java
  • 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()会内部对参数解码 的实现过程

  • 相关阅读:
    基于S3C2410的VIVI移植
    Android 多媒体
    target连上ubuntu,打adb shell后出现insufficient permissions for device错误
    git 学习
    ubuntu10.04中文乱码问题解决方法
    构建MINI2440开发板Ubuntu开发环境串口配置及使用
    如何解决:Android中 Error generating final archive: Debug Certificate expired on 10/09/18 16:30 的错误
    android Notification 学习
    Android declarestyleable:自定义控件的属性(attr.xml,TypedArray)的使用
    ubuntu 11.04 中的bug 渐显滚动条
  • 原文地址:https://www.cnblogs.com/ziq711/p/6266831.html
Copyright © 2011-2022 走看看