Handling garbled summary in Java Web Development

Source: Internet
Author: User

During development, it is inevitable that Chinese characters are displayed in disorder. Garbled text is a very difficult problem and often drives programmers crazy. Now I will share with you several solutions to solve Garbled text!

I. garbled processing of Form submission

Form submission is divided into get and post methods. The two methods have different garbled solutions. To use post for submission, you only need to add the request when accepting the request. setcharacterencoding ("UTF-8"); and The get method should break the received value into a ISO-8859-1 encoded, And Then assemble it into a UTF-8, new string (request. getparameter (PARAM ). getbytes ("iso8859-1"), "UTF-8 ");

Ii. garbled filter processing

If every receipt is processed as above, it will be too troublesome. It is very convenient to use a filter to handle request garbled characters. You only need to write a filter, you can filter out all request page garbled characters. For how to use a filter to handle Garbled text, see use a filter to solve JSP Chinese Garbled text. However, if struts is used, the filter for processing encoding in Web. xml must be registered before the struts filter; otherwise, all pages will not pass the filter.

Iii. garbled Ajax requests

If a garbled problem occurs when Ajax is used, the Chinese parameters in the request URL of the front-end Ajax function are processed using two encodeuri () functions. var url = "user. do? P = checkuser & name = "+ encodeuri (username. value), while the server uses urldecoder. the decode () function receives the message, string username = urldecoder. decode (request. getparameter ("name"), "UTF-8"); this can solve the problem of garbled characters in Ajax requests.

4. garbled link requests (IE6)

Garbled characters appear in IE6 when a link request is used to pass Chinese parameters. The solution is as follows: Use encodeuri (PARAM) to process Chinese parameters, such as location = "users. do? P = login & username = "+ encodeuri (username) +" & Password = "+ encodeuri (password );

To be continued .........

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.