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

Vue+spring boot realizes the verification code function

This article example shares the specific code of ...

Put frameset in body through iframe

Because frameset and body are on the same level, y...

Example of Html shielding right-click menu and left-click typing function

Disable right-click menu <body oncontextmenu=s...

Detailed explanation of the abbreviation of state in react

Preface What is state We all say that React is a ...

MySQL trigger detailed explanation and simple example

MySQL trigger simple example grammar CREATE TRIGG...

Solution to the horizontal scroll bar in iframe under IE6

The situation is as follows: (PS: The red box repr...

Determine whether MySQL update will lock the table through examples

Two cases: 1. With index 2. Without index Prerequ...

Steps to install Pyenv under Deepin

Preface In the past, I always switched Python ver...

The big role of HTML meta

There are two meta attributes: name and http-equiv...

Example code for implementing equal height layout in multiple ways with CSS

The equal height layout described in this article...

Solution to the problem of var in for loop

Preface var is a way to declare variables in ES5....

Layui implements the login interface verification code

This article example shares the specific code of ...