A debugging process record of Chinese garbled characters in the Tomcat source code startup console

A debugging process record of Chinese garbled characters in the Tomcat source code startup console

Find the problem

Today I am going to study the tomcat source code, so I downloaded the tomcat source code from the official website, imported it into IDEA, used the maven tool to build the project, started the project, and the console printed the tomcat log, but the Chinese characters were garbled.

At first I suspected it was a problem with IDEA, so I tried various solutions online. There are roughly these types:

1. Modify run/debug configurations and add VM options parameter: -Dfile.encoding=utf-8;

2. Modify run/debug configurations and add Environment variables parameters: JAVA_TOOL_OPTIONS:-Dfile.encoding=utf-8 and JAVA_OPTS:-Dfile.encoding=utf-8;

3. Modify the 3 encodings of IDEA configuration file encodings to UTF-8;

4. Modify IDEA's Custom VM options and add -Dfile.encoding=utf-8;

5. Modify the idea.exe.vmoptions and idea64.exe.vmoptions files in the bin directory of IDEA installation, and add -Dfile.encoding=utf-8;

6. Modify the encodings.xml file in the .idea folder under the project, and change it to UTF-8 instead of UTF-8;

7. Modify the logging.properties configuration file of tomcat and change the UTF-8 in it to GBK;

8. After modification, delete the target folder and recompile;

9. Restart IDEA after modification.

After trying all methods, the garbled console log problem was not solved, as shown in the figure:

After careful observation, I found that the Chinese garbled characters such as "Information" and "Serious" on the left side of the log have been resolved, but there are still garbled characters in the log.

I felt that it might be a problem with the code, so I decided to debug the code, starting with the first line of the log.

17-Feb-2020 10:10:08.585 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server.æœåŠ¡ Version: Apache Tomcat/@VERSION@

Find the log() method of the org.apache.catalina.startup.VersionLoggerListener class, set breakpoints and track step by step

Finally, it was found that all the values ​​​​existed in the lookup map collection of the PropertyResourceBundle class, and the data in the collection was garbled.

So continue to use the debugger to view the loading of the lookup, and check the source code to see that the data in the lookup collection is read from the properties file. Check that the properties file encoding is also UTF-8. So continue to view the source code.

The properties file loaded by is = classLoader.getResourceAsStream(resourceName); in ResourceBundle

Then load the data through the PropertyResourceBundle construction method.

When I was about to modify this code, I found that this was a class in JDK and could not be modified. (Later I learned that ResourceBundle is used for internationalization).

Later I checked the information and found out that in Java, the default format for reading files is iso8859-1, and when we store Chinese, it is usually UTF-8. So the result is garbled characters.

There are two solutions:

1. Use the native2ascii.exe tool under JDK to convert the properties file to Unicode encoding. After conversion, as shown below:

2. After getting the value in the code, manually re-encode and decode it

        try {

            value = new String(value.getBytes("ISO-8859-1"), "UTF-8");

        }catch(Exception e){

            e.printStackTrace();

        }

After testing, both methods can solve the problem.

Because there are too many properties files in Tomcat, I adopted the second method and modified the Tomcat source code as follows:

1) getString(final String key, final Object... args) method in org.apache.tomcat.util.res.StringManager class.

2) getMessage(String errCode) method of org.apache.jasper.compiler.Localizer class

At this point, the garbled problem is solved

Summarize

This is the end of this article about a debugging process record of Chinese garbled characters in the tomcat source code startup console. For more relevant Chinese garbled characters in the tomcat source code startup console, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • IntelliJ IDEA 2021 Tomcat 8 startup garbled code problem solution steps
  • How to improve Idea startup speed and solve Tomcat log garbled characters
  • Solve the Chinese garbled problem when Java & Idea start tomcat
  • Solution to the garbled output when IDEA starts Tomcat project
  • Solve the problem of garbled characters in Tomcat console when starting IDEA
  • How to solve the problem of a large amount of garbled characters when starting Tomcat

<<:  Use of MySQL triggers

>>:  Design Reference Beautiful and Original Blog Design

Recommend

CSS implements the web component function of sliding the message panel

Hello everyone, I wonder if you have the same con...

9 Practical CSS Properties Web Front-end Developers Must Know

1. Rounded Corners Today's web designs are con...

MySQL 8.0.2 offline installation and configuration method graphic tutorial

The offline installation method of MySQL_8.0.2 is...

Implementation code for using CSS text-emphasis to emphasize text

1. Introduction In the past, if you wanted to emp...

avue-crud implementation example of multi-level complex dynamic header

Table of contents Preface Background data splicin...

In-depth understanding of JavaScript callback functions

Table of contents Preface Quick Review: JavaScrip...

JavaScript dynamically generates a table with row deletion function

This article example shares the specific code of ...

Analysis of the process of building a LAN server based on http.server

I don’t know if you have ever encountered such a ...

Sample code for installing Jenkins using Docker

Two problems that are easy to encounter when inst...

Vue integrates PDF.js to implement PDF preview and add watermark steps

Table of contents Achieve results Available plugi...