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

Calendar effect based on jQuery

This article example shares the specific code of ...

Each time Docker starts a container, the IP and hosts specified operations

Preface Every time you use Docker to start a Hado...

Vue implements three-level navigation display and hiding

This article example shares the specific code of ...

How to solve the problem of case insensitivity in MySQL queries

question Recently, when I was completing a practi...

Detailed explanation of MySQL covering index

concept If the index contains all the data that m...

Vite+Electron to quickly build VUE3 desktop applications

Table of contents 1. Introduction 2. Create a Vit...

How to install Nginx and configure multiple domain names

Nginx Installation CentOS 6.x yum does not have n...

Detailed explanation of Strict mode in JavaScript

Table of contents Introduction Using Strict mode ...

Implementation of Nginx domain name forwarding https access

A word in advance: Suddenly I received a task to ...

MYSQL 5.6 Deployment and monitoring of slave replication

MYSQL 5.6 Deployment and monitoring of slave repl...

js returns to the previous page and refreshes the code

1. Javascript returns to the previous page history...

The difference between char, varchar and text field types in MySQL

In MySQL, fields of char, varchar, and text types...

MySql implements page query function

First of all, we need to make it clear why we use...

How to use vs2019 for Linux remote development

Usually, there are two options when we develop Li...