JSP virtual host use in garbled how to do

Source: Internet
Author: User
Tags tomcat

1 post Submission form is garbled

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

This situation, because the data submitted by the program received is garbled, this garbled and inserted into the database, so the display is not normal.

Solution:

A modify the preparation to complete

Modify the connectors in Server.xml for Tomcat's compounding file, plus uriencoding= "GB2312" is OK

b Write your own code conversion program

B.A when exchanging data with the form, do the conversion, this way flexible

Each page requests to write a conversion, or write a public class, at the time of receiving, do 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 filter defined in Tomcat's web.xml to convert all request encodings.

This needs to study the filter's writing, and then the specific conversion code, or B.A code to encode the conversion, but the transfer is not written in each program.

2 The database is garbled

That is, the data inside the database is garbled, no matter what code you use to connect the database, check to see are garbled. How do I determine if the database is garbled? (It's not easy to be sure, we just give a ballpark estimate).

When you use the client to connect the database, must choose the connection code for GB2312,UTF-8,ISO-8859-1 and so on common coding format, connect and see your table content is not normal, if not a situation is normal, should be able to determine for garbled.

Of course, this decision is not standard, even a lot of problems, but at home, I think more than 99% of these codes. So I think the accuracy of this decision is acceptable.

Solution: When you use the client to connect to the database, be sure to select the connection code as GB2312 or GBK, and then rerun the database script to ensure that the database is keeping the normal characters. Not garbled.

3 extracted from the database is garbled

The database is normal, but with the Java connection, once extracted, it's all messed up.

Solution: Modify the URL of the Java Connection database, add or modify the URL in the encoding for Utf-8,characterencoding=utf-8, if hibernate configuration problem, JDBC Connection URL can not have & symbol, will cause an error or not take effect, I'll use & instead.

Do not think that use is GB2312 here is designated as GB2312 (personally think if the designation for GB2312, drive and do a code conversion, so it became garbled again).

Of course, it may also drive too old and so on.

Above only said a few common, garbled problem more than 95% of the above should be mentioned above, there are many situations, you need to analyze your own slowly.

Related Article

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.