Perfect solution to get and POST request Chinese garbled problem _java

Source: Internet
Author: User

For post requests, just write the following code in the servlet or JSP to resolve the Chinese garbled problem passed in from the form

Request.setcharacterencoding ("Utf-8");

For GET requests, because the request parameters are appended to the URL of the address bar, you cannot use the above processing method. It should be like this:

String str=request.getquerystring ();

Use Urldecoder to decode strings string

str1=java.net.urldecoder.decode (str, "Utf-8");

String[] Parastrings=str1.split ("&");

Parastrings[0] is the first argument, and so on

... for (String parastring:parastrings)

{

 string[] namevalue=parastring.split ("=");

 Namevalue[0] is the form's name,namevalue[1] is the value of the form name

Another method is to encode the request parameter value after obtaining the request parameter, that is, converting it to a byte array, then decoding the byte array into a string.

String str=request.getparameter ("name");

Byte[] Bytes=str.getbytes ("iso-8859-1");

String name=new string (bytes, "Utf-8");

The above perfect solution to get and POST request Chinese garbled problem is small series to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.