从apache tomcat下载tomcat8源码
1.下载到源码后,tomcat默认使用ant作为包管理工具,本地调试时创建pom.xml, 手动创建一个pom.xml放入源码根目录- <?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>Tomcat8.0</artifactId>
- <name>Tomcat8.0</name>
- <version>8.0</version>
-
- <build>
- <finalName>Tomcat8.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>2.0.2</version>
-
- <configuration>
- <encoding>UTF-8</encoding>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <dependency>
- <groupId>org.easymock</groupId>
- <artifactId>easymock</artifactId>
- <version>5.0.0</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.unboundid</groupId>
- <artifactId>unboundid-ldapsdk</artifactId>
- <version>6.0.4</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>ant</groupId>
- <artifactId>ant</artifactId>
- <version>1.7.0</version>
- </dependency>
- <dependency>
- <groupId>ant</groupId>
- <artifactId>ant-apache-log4j</artifactId>
- <version>1.6.5</version>
- </dependency>
- <dependency>
- <groupId>ant</groupId>
- <artifactId>ant-commons-logging</artifactId>
- <version>1.6.5</version>
- </dependency>
- <dependency>
- <groupId>wsdl4j</groupId>
- <artifactId>wsdl4j</artifactId>
- <version>1.6.2</version>
- </dependency>
- <dependency>
- <groupId>javax.xml.rpc</groupId>
- <artifactId>javax.xml.rpc-api</artifactId>
- <version>1.1</version>
- </dependency>
- <dependency>
- <groupId>org.eclipse.jdt.core.compiler</groupId>
- <artifactId>ecj</artifactId>
- <version>4.6.1</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
复制代码 2.使用idea导入tomcat源码 File->Project from Existing Sources -> Maven import
重新导入依赖后TestCookieFilter会报错,本地注释掉单元测试类即可。
3.编辑配置文件, Edit Configuration, 如下图。
设置catalina.home和tomcat启动类Bootstrap, jdk我选择的是1.8
4.Maven Reload, 把依赖的包重新导入进来。
添加JSP解析器代码:
1.Servlet.service() for servlet [jsp] in context with path [] threw exception [org.apache.jasper.JasperException: Unable to compile class for JSP] with root cause
解决方案:编辑 org.apache.catalina.startup.ContextConfig 文件的 configureStart() 方法,添加初始化 JSP 解析器的代码:
在ContextConfig类的configStart方法中加入以下代码- context.addServletContainerInitializer(new JasperInitializer(), null);
复制代码
我本地下载的tomcat8源码, 以上工作准备完后,直接编译,单元测试也没报错。运行起来,就可以看到猫咪了, 可以开心的看源码了。
我本地的源码config/server.xml里配置的是8080端口, 这里就跑起来了。
tomcat源码目录结构
tomcat核心源码
CATALINA_HOME和CATALINA_BASE
开始阅读tomcat源码之前,先浏览一遍tomcat官方写的介绍,里面内容很全,也是我们学习的第一手资料,这里涉及到两个变量CATALINA_HOME和CATALINA_BASE。
- CATALINA_HOME:表示 Tomcat 安装的根目录,即D:\Gitee\tomcat8\apache-tomcat-8.5.85-src
- CATALINA_BASE:表示特定 Tomcat 实例的运行时配置的根。如果你需要在一台机器上配置多个tomcat实例,则需要用到catalina_base变量
为什么使用CATALINA_BASE
默认情况下,CATALINA_HOME和CATALINA_BASE指向同一目录。当您需要在一台计算机上运行多个 Tomcat 实例时,手动设置CATALINA_BASE。这样做具有以下优点:
- 更轻松地管理升级到较新版本的 Tomcat。由于具有单个CATALINA_HOME位置的所有实例共享一组文件和二进制文件,因此您可以轻松地将文件升级到较新版本,并将更改传播到使用同一CATALIA_HOME目录的所有 Tomcat 实例。
- 避免重复相同的bin、lib文件夹的文件。
- 共享某些设置的可能性,例如 shell 或 bat 脚本文件(取决于您的操作系统)。
参考资料
https://2i3i.com/tomcat-code-7.html
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |