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="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>com.github.sources</groupId> <artifactId>source-tomcat</artifactId> <version>9.0.26</version> <name>source-tomcat</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.10.1</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>javax.xml</groupId> <artifactId>jaxrpc</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.eclipse.jdt</groupId> <artifactId>org.eclipse.jdt.core</artifactId> <version>3.18.0</version> </dependency> <dependency> <groupId>org.eclipse.jdt.core.compiler</groupId> <artifactId>ecj</artifactId> <version>4.6.1</version> </dependency> </dependencies> <build> <finalName>Tomcat9.0</finalName> <sourceDirectory>java</sourceDirectory> <testSourceDirectory>test</testSourceDirectory> <resources> <resource> <directory>java</directory> </resource> </resources> <testResources> <testResource> <directory>test</directory> </testResource> </testResources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <encoding>UTF-8</encoding> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
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 127.0.0.1:8080 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 127.0.0.1:8080 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.
Blog:
- Jane book: https://www.jianshu.com/u/91378a397ffe
- csdn: https://blog.csdn.net/ZhiyouWu
- Open Source China: https://my.oschina.net/u/3204088
- Nuggets: https://juejin.im/user/5b5979efe51d451949094265
- Blog Garden: https://www.cnblogs.com/zhiyouwu/
- Wechat Public Number: Source Bay
- Wechat: WZY1782357529 (Welcome to Communication)