In Java-based programming, there is often a problem with the Chinese characters and display, such as a lot of garbled or question marks.
This is because the default encoding mode in Java is Unicode, and the documents and DBs commonly used by Chinese people are based on GB2312 or BIG5 and other codes, so this problem will occur. I used to worry about this problem, and later I checked some information, I finally solved it. I know that there must be many friends will encounter this problem, so I have summed up, come out to let everyone share it.
1. Output Chinese in the web page.
Java used in network transmission is "ISO-8859-1", so transformation is required when output, such as:
String str = "Chinese";
Str = new string (Str.getbytes ("GB2312"), "8859_1");
However, if you use the code when the program is compiled, the encoding is "GB2312", and this program is run on the Chinese platform, and this problem will not be noted.
2, read Chinese from the parameters
This is just the opposite of the output in the web page, as:
Str = new string (str.getbytes ("8859_1"), "GB2312");
3. Operate the Chinese problem in DB
A simpler way is to set the "region" as "English (US)" in "Control Flag". If there will be garbled, you can also make the following settings:
Take Chinese: str = new string (str.getbytes ("GB2312");
Enter Chinese in DB: str = new string (str.getbytes ("ISO-8859-1");
4. Chinese resolution in JSP:
In "Control Field," region "is set to" English (US) ".
Add: in the JSP page:
If it is not normal, the following conversion is required:
Such as: name = new string (Name.getbytes ("ISO-8859-1"), "GBK");
There will be no Chinese problem.