JSP in the request object to solve Chinese garbled

Source: Internet
Author: User

If the parameter value is in Chinese when the request parameter is obtained through the requests object, the obtained parameter will be garbled if it is not processed. In JSP, to solve the request parameters are the Chinese garbled situation, can be divided into the following two kinds:

1. Get access Request parameter garbled

When the access request parameter is Chinese, the Chinese parameter value obtained through the object is garbled because the request parameter is ISO-8859-1 encoded and does not support Chinese. Therefore, you can display Chinese correctly only if you reconstruct a string object using the UTF-8 or GBK encoding of the acquired data through string construction. For example, when you get the parameter user that includes Chinese information, you can use the following code:

<span style= "FONT-SIZE:18PX;" >string user=new String (request.getparameter ("user"). GetBytes ("Iso-8859-1"), "Utf-8");</span>
2. Get the information of the form submission garbled

The Chinese parameter value obtained through the request object is garbled when the form's information is obtained. This can be done by setting the encoding to UTF-8 or GBK by adding the setcharacterencoding () method that invokes the request object under the page directive. For example, when you get the value of a user name text box that includes Chinese information (the Name property is username), you can add the following code before you get all the form information:

<span style= "FONT-SIZE:18PX;" ><%
    request.setcharacterencoding ("UTF-8");
%></span>
In this way, the following code to get the value of the form, it will not produce Chinese garbled.

<span style= "FONT-SIZE:18PX;" >string user =request.getparameter ("username");</span>




Related Article

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.