Recently these two days changed two items of bugs, have encountered the problem of Chinese garbled, now the problem and solutions are summarized here for your reference.
If at first you put the entire project coding and tomcat coding are set to the same, such as GBK or GB2312 or UTF-8, should be the interaction between the front and back will not be a problem, but there will be like me in the middle of the project to add functionality, and then let the Chinese garbled, this does not know what caused the cause of the Only know how to solve.
Issue 1: Incoming reception from the background Chinese garbled.
Solution: Before you process data, set up in the background
Response.setcharacterencoding ("UTF-8"); The code here can be a little bit of a try, common utf-8,iso-8859-1,gb2312
The front desk also wants to set the code: set in head
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
Question two: Incoming background from the foreground Chinese garbled
Solution: This problem is more headache, I now have three kinds of problems can be solved.
1) Set the request
Request.setcharacterencoding ("Utf-8"); same as setting response.
2) Convert the character into a stream of characters to transcode, the use of Chinese strings from the front desk to do the following, the code in the way varies from person to person
New String (Str.getbytes ("GBK"), "Utf-8");
3) transcoding using the Stringutil class
We'll talk about this solution tomorrow.
I have something to say about the problem of Java Chinese garbled.