ServletRequest的一个小知识点

看到有人直接通过ServletRequest.getInputStream()中读取Post中的body,代码大概如下:

 

        String charset = request.getCharacterEncoding(); 
         if (charset == null) { 
             charset = DEFAULT_ENCODE; 
         } 
         BufferedReader in = new BufferedReader(new  InputStreamReader(request 
                 .getInputStream(), charset)); 
  
         // Read the request 
         CharArrayWriter data = new CharArrayWriter(); 
         char[] buf = new char[8192]; 
         int ret; 
         while ((ret = in.read(buf, 0, 8192)) != -1) { 
             data.write(buf, 0, ret); 
         } 
         return data.toString(); 

 

当时心里有个疑问:为什么不直接通过调用request.getParameter()来获取相应的值呢?虽然至今也没想到答案,但是通过查看servlet规范了解到了ServletRequest的一个小知识,规范原文如下:

 

SRV.3.1.1 When Parameters Are Available
The following are the conditions that must be met before post form data will
be populated to the parameter set:
1. The request is an HTTP or HTTPS request.
2. The HTTP method is POST.
3. The content type is application/x-www-form-urlencoded.
4. The servlet has made an initial call of any of the getParameter family of methods
on the request object.
If the conditions are not met and the post form data is not included in the
parameter set, the post data must still be available to the servlet via the request
object’s input stream. If the conditions are met, post form data will no longer be
available for reading directly from the request object’s input stream.

 

英文比较简单,就不翻译了。

 

 

 

补充:对之前问题“为什么不直接通过调用request.getParameter()来获取相应的值呢”的解答

 

不使用request.getParameter()来获取,而是在自己业务逻辑代码中处理有以下几种可能:
1、request的body内容不是“参数名=参数值”的形式,而单纯是一段文字或者二进制字节流,这样就不可能通过request.getParameter()来获取参数值;

2、如果request的body内容的格式是固定的,那么在程序代码中可以针对这种特定的格式进行优化处理;

3、如果request的body内容比较长,并且网络环境又不好,那么直接调用request.getParameter()将会阻塞较长时间。并且如果body的内容允许边接受边处理的话(比如xml),那么通过自己的业务逻辑代码来处理body内容效率将会更高;


以上答案仅仅是本人的理解,仅供参考。
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值