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 .........