Tomcat Source Analysis I: Compiling Tomcat Source Code

Keywords: Java Tomcat Apache Maven xml

Tomcat Source Analysis I: Compiling Tomcat Source Code

1 Content Introduction

In the previous "Servlet and Tomcat Running Example" article, we will show you how to deploy Servlet application in Tomcat. This article will start Tomcat source code analysis journey on the basis of the above, I will analyze the starting process and operating principle of Tomcat in detail. This article will be the most basic lesson, that is, to compile Tomcat source code locally, to lay the foundation for the following analysis!

2 Compile Tomcat source code

2.1 Download Tomcat source code

We download the latest Tomcat source package on Tomcat's official website. The latest version is 9.0.26. We download the tar.gz version of its source package, as follows:

2.2 decompression source package apache-tomcat-9.0.26-src.tar.gz

After decompressing the source package apache-tomcat-9.0.26-src.tar.gz, the contents are as follows:

2.3 Add pom.xml to the decompressed folder

To import the Tomcat project in Maven mode, you need to add the corresponding maven dependencies. Here you add the pom.xml file, which reads as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""




2.4 IDEA Importing tomcat Source Engineering

Using IDEA development tools, tomcat project is imported in Maven mode. After importing, the project structure is as follows:

2.5 Start Tomcat Project

main method of running Bootstrap class under org.apache.catalina.startup package

At this time, we found some errors. Now we can solve these errors.

3. Solution of Abnormal Problem

3.1 Tralers. ResponseTrailers do not exist

We can find this class in webapps/examples/WEB_INF/classes/trailers directory. We will copy this class to test:

Following the completion of the copy, the situation is as follows:

3.2 CookieFilter does not exist

Similarly, we will copy the home webapps examples WEB-INF classes util CookieFilter. java file to the test util directory:

3.3 FileNotFoundException: /Library/ApacheTomcat/source/test/source-tomcat/conf/server.xml (No such file or directory)

After solving the problems mentioned above in 3.1 and 3.2, the following problems arise:

  • Solution:
    In the startup configuration, add the parameters of VM options, add the project path, the local machine is / Library / Apache Tomcat / source / test / Apache - Tomcat - 9.0.26 - src, so add the parameters as follows:
    - Dcatalina.home=/Library/Apache Tomcat/source/test/apache-tomcat-9.0.26-src, as shown below:

3.4 java.lang.ClassNotFoundException: listeners.ContextListener

After solving the above 3.3 problem, the main method of Bootstrap class is started again. The program has the following error message:

  • Solution: Delete the examples folder under webapps! The program runs again without reporting this error!

3.5 Servlet.service() for servlet [jsp] in context with path [] threw exception [org.apache.jasper.JasperException: Unable to compile class for JSP] with root cause

After solving the above problems, we start the main method of Bootstrap class and the program starts normally. At this time, we access in the browser. The program has the following error message:

  • Solution: Edit the configureStart() method of the org.apache.catalina.startup.ContextConfig file and add the code to initialize the JSP parser:
context.addServletContainerInitializer(new JasperInitializer(), null);

After adding, the main method is started again. The browser enters and the result is Tomcat's interface:

3.6 Log Scrambling

Start the log contains a lot of scrambling code, although it does not affect the overall program, but after looking at the log, it still has a greater impact, first look at the log scrambling situation:

  • Solution:
    Modify the content of vm options and set the environment to US-English as follows:
-Duser.language=en -Duser.region=US -Dfile.encoding=UTF-8

Run the main method again, and the program log will display normally:

So far, we have imported Tomcat's source code into IDEA tools, and solved some problems. After that, we will use this source code to analyze Tomcat's start-up and operation principle.


Posted by [JayZ] on Fri, 11 Oct 2019 01:01:54 -0700