简体   繁体   中英

Reading post data from servlet

I'm trying to read post data from a servelet doPost method the following way.

  1. using httpservletrequest.getInputStream();

  2. Creating an instance of bytearrayoutputstream .

  3. writing the post data to bytearrayoutputstream from httpservletrequest.getInputStream() ;

  4. Finally, output I'm getting from bytearrayoutputstream.toByteArray() .

The problem with that is, when I enter 150/10 in the textfield, toByteArray gives me 150%2F10 . toByteArray seems to be encoding the special characters in the output for the / character.

What will be the elegant way to read post data from servlet doPost() method?

before you pass data to the servlet, try to encode it in client side. or else when you retrieve it, in the servlet, do a Url encode there.

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();

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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