What should I do if garbled characters appear in JSP VM usage?

Source: Internet
Author: User

1. The POST submission form is garbled.

The common situation is that the page is normal, but the newly inserted data is garbled.

In this case, it is because the submitted data is garbled after receiving by the program, and the garbled code is inserted into the database, so the display is abnormal.

Solution:

A. modify the configuration to complete

Modify the connector in the configuration file server. xml of tomcat and add URIEncoding = "GB2312 ".

B. Write the Code Conversion Program by yourself.

B. a converts data with the form, which is flexible.

Every page request writes a conversion, or writes a public class. When receiving the code, it performs a transfer. The Code is as follows:

Public static String ISOGBChange (String s)

{

Return EncodeChange (s, "ISO8859-1", "GB2312 ");

}

Public static String EncodeChange (String s, String source_encode, String dest_encode)

{

If (s = null)

Return null;

Try

{

Byte [] tmpbyte = s. getBytes (source_encode); s = new String (tmpbyte, dest_encode );

Return s;

}

Catch (Exception e)

{

Return "ERROR ";

}

}

B. B uses the filter defined in tomcat web. xml to convert all request codes.

This requires you to study the filter writing method, the specific conversion encoding, or the code in B. a for encoding conversion, but the transfer does not need to be written in every program.

2. The database is garbled.

That is to say, the data in the database is garbled. No matter what encoding you use to connect to the database, all the data is garbled. How can you determine whether the database is garbled? (In fact, it is not easy to determine. We only give a rough judgment ).

When you use the client to connect to the database, be sure to select the connection encoding for GB2312, UTF-8, ISO-8859-1 and other common encoding formats, connect and view your table content is not normal, if none of the above conditions is normal, it can be identified as garbled characters.

Of course, this determination is not standard, and there are even a lot of problems, but in China, I think more than 99% of them use these encodings, so I think this determination accuracy is acceptable.

Solution: when you use a client to connect to the database, You must select GB2312 or GBK as the connection code, and then re-execute the database script to ensure that the database stores normal characters. instead of garbled characters.

3. garbled characters are extracted from the database.

It is normal in the database, but after connecting with JAVA, Once extracted, it is all messy.

Solution: Modify the JAVA Connection database URL, plus or modify the URL encoding for UTF-8, characterEncoding = UTF-8, if hibernate configuration problems, jdbc connection url cannot have & symbol, it will cause errors or will not take effect later. I can use & instead.

Do not specify GB2312 as GB2312 here (I personally think that if GB2312 is specified, the driver will perform another encoding conversion, so it becomes garbled again ).

Of course, the driver may be too old.

The above is just a few common examples. more than 95% of the garbled problem should be mentioned above. There are still many situations that require you to analyze them slowly.

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.