简体   繁体   English

从servlet读取发布数据

[英]Reading post data from servlet

I'm trying to read post data from a servelet doPost method the following way. 我正在尝试通过以下方式从servelet doPost方法读取发布数据。

  1. using httpservletrequest.getInputStream(); 使用httpservletrequest.getInputStream();

  2. Creating an instance of bytearrayoutputstream . 创建一个bytearrayoutputstream实例。

  3. writing the post data to bytearrayoutputstream from httpservletrequest.getInputStream() ; 将发布数据从httpservletrequest.getInputStream()写入bytearrayoutputstream

  4. Finally, output I'm getting from bytearrayoutputstream.toByteArray() . 最后,我从bytearrayoutputstream.toByteArray()获得输出。

The problem with that is, when I enter 150/10 in the textfield, toByteArray gives me 150%2F10 . 问题是,当我在文本字段中输入150/10时, toByteArray给我150%2F10 toByteArray seems to be encoding the special characters in the output for the / character. toByteArray似乎在对/字符的输出中的特殊字符进行编码。

What will be the elegant way to read post data from servlet doPost() method? 从servlet doPost()方法读取帖子数据的优雅方法是什么?

before you pass data to the servlet, try to encode it in client side. 在将数据传递到servlet之前,请尝试在客户端对其进行编码。 or else when you retrieve it, in the servlet, do a Url encode there. 否则,当您在servlet中检索它时,在此处进行Url编码。

try this way: 尝试这种方式:

String abc = URLEncoder.encode("convert my string 150/10", "UTF-8");
byte[] barr = abc.getBytes(Charset.forName("UTF-8"));

use above with the 与以上配合使用

request.getParameter(); request.getParameter();

then you don't have to play with all these fancy codes. 那么您不必使用所有这些花哨的代码。 I guess when you convert into a byte array, you will have to specify the encoding mechanism to avoid char set ambiguity. 我猜想当您转换为字节数组时,必须指定编码机制,以避免字符集不明确。

I did not try the above code, but would like you to have a try! 我没有尝试上面的代码,但希望您尝试一下!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM