The principle and solution of Tomcat Chinese garbled problem, and the principle of tomcat Chinese garbled Problem

Source: Internet
Author: User

The principle and solution of Tomcat Chinese garbled problem, and the principle of tomcat Chinese garbled Problem

Since its access to Java and JSP, it has been constantly dealing with Java's Chinese garbled characters, and now it has finally been completely solved. We will share our solutions with you.

I. Why Java Chinese problems

The Java kernel and class files are unicode-based, which makes Java programs have good cross-platform performance, but it also brings some trouble about Chinese garbled characters. There are two main reasons: the garbled problem generated during compilation of Java and JSP files and the garbled problem caused by interaction between Java programs in other media.

First, Java (including JSP) source files may contain Chinese characters, while Java and JSP source files are stored Based on byte streams. If Java and JSP are compiled into class files, if the encoding method used is inconsistent with the source file encoding, garbled characters may occur. Based on this Garbled text, it is recommended that you do not write Chinese characters in the Java file as much as possible (the comments do not participate in compilation, and it is okay to write Chinese characters). If necessary, manually include the-ecoding GBK or-ecoding gb2312 parameter. for JSP, add <% @ page contentType = "text/html" to the file header; charset = GBK "%> or <% @ page contentType =" text/html; charset = gb2312 "%> can basically solve this type of garbled problem.

This article focuses on the second type of Garbled text, that is, the Garbled text generated when Java programs interact with other storage media. Many storage media, such as databases, files, and streams, are stored Based on byte streams. When Java programs interact with these media, character (char) and byte (byte) occur) for example, the data submitted from the Page Submission Form Shows garbled characters in the Java program.

If the encoding method used in the above conversion process is inconsistent with the original Byte encoding, garbled characters may occur.

Ii. Solution

For popular Tomcat, there are two solutions:

1) Change D: \ Tomcat \ conf \ server. xml and specify the encoding format of the browser as "simplified Chinese ":

The method is to find

<Connector port = "8080" maxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
EnableLookups = "false" redirectPort = "8443" acceptCount = "100"
ConnectionTimeout = "20000" disableUploadTimeout = "true"URIEncoding = 'gbk'/>

Mark, which is added in bold.

You can verify whether your change is successful in this way: before the change, click "View | encoding" in the IE browser on the page where you are garbled, and you will find "Western Europe (ISO) "is selected. After the change, click "View | encoding" in the menu and you will find that "simplified Chinese (GB2312)" is selected.

B) For this Java program, my program is like this:

Public class ThreeParams extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html; charset = GBK ");
...
}
}

The bold Chinese character is required. It enables the browser to convert Unicode characters to GBK characters. In this way, the content of the page and the display mode of the browser are set to GBK, so there will be no garbled characters.

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.