Solutions to garbled data reading in Jsp programs or writing data to DB

Source: Internet
Author: User

Solutions to garbled data reading in Jsp programs or writing data to DB
In JAVA-based programming, Chinese characters are frequently encountered and displayed, such as a lot of garbled characters or question marks.
This is because the default encoding method in JAVA is UNICODE, and the files and DB commonly used by Chinese people are based on GB2312 or BIG5 encoding, so this problem occurs. In the past, I often worried about this problem. After checking some information, I finally solved the problem. I know that many of my friends will also encounter this problem, so I just summarized it, to share with you.
1. Output Chinese characters on the webpage.
JAVA is used in network transmission encoding is "ISO-8859-1", so in the output need to be converted, such:
String str = "Chinese ";
Str = new String (str. getBytes ("GB2312"), "8859_1 ");
However, if the encoding used during program compilation is "GB2312" and the program runs on the Chinese platform, this problem does not occur. Be sure to pay attention to this problem.
2. Read Chinese from Parameters
This is exactly the opposite of the output in the webpage:
Str = new String (str. getBytes ("8859_1"), "GB2312 ");
3. Questions about how to operate databases in Chinese
A simple method is to set "region" to "English (USA)" in "control plane )". If garbled characters still appear, you can perform the following settings:
When retrieving Chinese characters: str = new String (str. getBytes ("GB2312 "));
Input Chinese characters to DB: str = new String (str. getBytes ("ISO-8859-1 "));
4. Solutions for Chinese in JSP:
In "control plane", set "region" to "English (USA )".
Add:
If not, perform the following conversion:
For example: name = new String (name. getBytes ("ISO-8859-1"), "GBK ");
There will be no Chinese problems.

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.