In the JSP program read or write data to db garbled solution
Source: Internet
Author: User
js| Program | solve | data in JSP program read or write data to db garbled solution
In Java based programming, often encountered in the Chinese characters in the place and display problems, such as a lot of garbled or question marks.
This is because the default encoding in Java is Unicode, and the file and db used by the Chinese usually are based on GB2312 or BIG5 encoding, and this problem occurs. I used to be worried about this problem, and then after the investigation of some information, finally resolved, I know there must be a lot of friends will encounter this problem, so special summed up a bit, to take out for everyone to share.
1, the output of Chinese in the Web page.
Java is used in the network transmission code is "iso-8859-1", so the output needs to be transformed, such as:
String str= "Chinese";
Str=new String (str.getbytes ("GB2312"), "8859_1");
However, if you are compiling the program with the encoding "GB2312" and running this program on the Chinese platform, you must be aware that this problem does not occur.
2. Read Chinese from parameters
This is exactly the opposite of the output in the Web page:
Str=new String (str.getbytes ("8859_1"), "GB2312");
3, operating db in the Chinese problem
A simpler approach is to set the area to English (United States) in Control Panel. If there will be garbled, you can also set the following:
When taking Chinese: str=new String (Str.getbytes ("GB2312"));
Enter Chinese in DB: Str=new String (Str.getbytes ("iso-8859-1"));
4, in the JSP in the Chinese solution:
In Control Panel, set the area to English (United States).
In the JSP page, add:
If the display is not working correctly, the following conversions are also performed:
such as: Name=new String (Name.getbytes ("iso-8859-1"), "GBK");
There will be no Chinese problems.
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.