Tomcat source code learning environment construction

Time:2021-5-10

1、 Source code download

PS: multi map warning

Before we start to read the source code, we need to build an environment to facilitate us to debug the source code. We can download the specific source code from the official website (here I take version 8.5.63 as an example).

Tomcat source code learning environment construction

2、 Project import

Download and unzip apache-tomcat-8.5.63-src.zip.

Tomcat source code learning environment construction

Then enter the apache-tomcat-8.5.63-src directory and add a pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

    <groupId>org.apache.tomcat</groupId>
<artifactId>apache-tomcat-8.5.63-src</artifactId>
    <version>8.5</version>
    <name>Tomcat8.5</name>

<!--  Tomcat basic dependency package -- >
<dependencies>
    <!--  Remote procedure call toolkit -- >
    <dependency>
        <groupId>javax.xml</groupId>
        <artifactId>jaxrpc</artifactId>
        <version>1.1</version>
    </dependency>
    <!--  Soap protocol processing toolkit -- >
    <dependency>
        <groupId>javax.xml.soap</groupId>
        <artifactId>javax.xml.soap-api</artifactId>
        <version>1.4.0</version>
    </dependency>
    <!--  A tool for parsing web service's WSDL file -- >
    <dependency>
        <groupId>wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
        <version>1.6.2</version>
    </dependency>
    <!--  Eclipse java compiler -- >
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.5.1</version>
    </dependency>
    <!--  Ant management tools -- >
    <dependency>
        <groupId>ant</groupId>
        <artifactId>ant</artifactId>
        <version>1.7.0</version>
    </dependency>
    <!--  Easymock auxiliary unit test -- >
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.4</version>
    </dependency>
</dependencies>

    <build>
        <!--  Specify source directory -- >
        <finalName>Tomcat8.5</finalName>
        <sourceDirectory>java</sourceDirectory>
        <resources>
            <resource>
                <directory>java</directory>
            </resource>
        </resources>
        <plugins>
            <!--  Introduce the compiler plug-in, specify the compiler level and code -- >
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Introducing projects through idea

Tomcat source code learning environment construction

After opening the project, search the main method globally to find the entrance to Tomcat

Tomcat source code learning environment construction

There are six main methods, including a bootstrap class, which translates to bootstrap. Let’s jump to this class and run to see if there is any problem.Tomcat source code learning environment construction

The project is started successfully. The information output from the console is garbled. For the time being, let’s see if we can access the home page.

Tomcat source code learning environment constructionDisplay 500 status code, give a lot of information is messy, then let’s see what causes it.

3、 The cause of garbled code

Find the place where the garbled code appears at the beginning, select the class name, double-click the shift key to find and jump to the corresponding method.

Tomcat source code learning environment construction

Tomcat source code learning environment construction

Make a breakpoint and startDEBUGFirst step in one layer at a time and follow up to see where the first garbled code isTomcat source code learning environment construction

Here we can see, throughhandleGetObject()Method to get the object is the case of garbled code, hit a breakpoint here, and then continue to deepen

First layer by layer into the track to see where the first chaos is

//Debug jump line
VersionLoggerListener.java
private void log(){ ... }

    ↓

StringManager.java
public String getString(String key){ ... }

    ↓

ResourceBundle.java
public final Object getObject(String key){ 
    Object obj = handleGetObject(key);
    ...
}

    ↓

protected abstract Object handleGetObject(String key);

    ↓

PropertyResourceBundle.java
public Object handleGetObject(String key){
    return lookup.get(key);
}

protected abstract Object handleGetObject(String key);This is an abstract method, clickSetp intoButton to jump into the corresponding specific implementation methodTomcat source code learning environment construction

In this method, we can see that he is actually in aHashMapAccording to thekeyTo get the correspondingvalue, select the code block and pressAIT + F8Check it outTomcat source code learning environment construction

We found the cause, right hereHashMapStored invalueAre garbled, will lead to later call to the place is displayed garbledTomcat source code learning environment construction

If the cause is found, then we have to look for thisHashMapObjectputWhere the operation is carried out can really solve the problem.

choicelookupPressCtrl + FFind out the two methods, break them, and restart the project to see which method is used.Tomcat source code learning environment construction

Here we can see that it is implemented in the form of input stream, which is shown belowVariablesYou can see it in the windowPropertiesThe loaded data is garbledTomcat source code learning environment construction

stayFramesIn the window, click the previous stack frame to find the calling entry of the current method, and trace back to find itstreamThe source ofTomcat source code learning environment constructionTomcat source code learning environment constructionTomcat source code learning environment construction

Find the corresponding file according to the path given above, and you can see that there areUTF-8Encoded Chinese characters.Tomcat source code learning environment construction

There is no problem with file coding, so the problem lies inis = classLoader.getResourceAsStream(resourceName);This method is used. At this time, we want to modify it. We find that this class is locked and cannot be modified. We can only see that it cannot be touched. It’s a bit difficult~

4、 Solve the garbled code

We have to go over the configuration information again to see which class can be edited and then modify it

//Debug jump line
VersionLoggerListener.java
private void log(){ ... }

    ↓

StringManager.java
public String getString(String key){ ... }

We can see it againStringManagerIn this class, there is a method to obtain, and then go down to lock class. So we try to code the result in this layer to see how it worksTomcat source code learning environment construction

Tomcat source code learning environment construction

Effective. The information displayed on the console is no longer garbled. Let’s take a look at the pageTomcat source code learning environment construction

Tomcat source code learning environment construction

There is still a part of the page display is garbled, it seems that there are still places to be modified, continue to lookTomcat source code learning environment construction

By viewing the error information of the console, search the location of the prompt globally to locate itTomcat source code learning environment construction

Jump to the corresponding position,CTRL + leftNavigate to the call, as you can see hereerror()There are some specific information parameters in the method. We use the old method to analyze thise.getMessageLet’s modify this parameterTomcat source code learning environment constructionTomcat source code learning environment construction

Console garbled solved, but the page here or garbled, it seems to find the wrong place, again!Tomcat source code learning environment constructionTomcat source code learning environment construction

Find it according to the prompts on the pageJspCompilationContextClasscompileMethod, line by line to find the errorTomcat source code learning environment construction

See here’s oneLocalizer.getMessage()Method is used to obtain information. Jump in and have a look. It’s really here. If you find a place, do it directly!Tomcat source code learning environment constructionTomcat source code learning environment construction

Restart the project, refresh again to see the effect, finally done, not easy~Tomcat source code learning environment construction

5、 Solve the “unable to compile class for JSP” exception

What causes the page to display the 500 status codeTomcatJSP engine in source codeJasperIt is not initialized, so it is unable to compile and process JSP (I think JSP needs to be converted into servlet for further compilation). We only need toTomcatSource code ofContextConfigClassconfigureStartMethod to initialize the engine. The code is as follows:

context.addServletContainerInitializer(new JasperInitializer(), null); 

Tomcat source code learning environment construction

Now big announcement chengla!!!Tomcat source code learning environment construction

I have a large collectionComputer electronic booksIf you need help, pick it up~

Recommended Today

Learn a Linux command every day (62): Ping

Yesterday’s recommendation:Learn a Linux command every day (61): WGet Command introduction The ping command is used to test the network connectivity between hosts. Ping is a simple way to send network data to and receive network data from another computer on the network. It is usually used to test whether it can be accessed through […]