- 相关:
- 搭建好的tomcat编译环境 - https://gitcode.net/LawssssCat/learn-tomcat
文档: https://tomcat.apache.org/tomcat-8.0-doc/building.html
源码: apache-tomcat-8.5.84-src - https://tomcat.apache.org/download-80.cgi
-
源码解压 E:\temp1\apache-tomcat-8.5.84-src (记为${tomcat.src})
-
解压文件夹根目录下创建 ${tomcat.src}/pom.xml (并引入依赖)
💡 提示
依赖的声明(特别是版本的)位置 ${tomcat.src}/build.properties.default
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.tomcat</groupId> <artifactId>apache-tomcat</artifactId> <packaging>jar</packaging> <version>8.5.42</version> <name>Tomcat8.5</name> <url>https://tomcat.apache.org/download-80.cgi</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <build.outputDirectory>output</build.outputDirectory> </properties> <!-- 编译设置 --> <build> <finalName>${project.name}</finalName> <sourceDirectory>java</sourceDirectory> <!-- 测试环境特别难搞,而且发行版一般都是通过测试的(只要环境ok),并且我们只是看源码,不需要测试,因此跳过测试 --> <!-- <testSourceDirectory>test</testSourceDirectory> --> <outputDirectory>${build.outputDirectory}/classes</outputDirectory> <testOutputDirectory>${build.outputDirectory}/test-classes</testOutputDirectory> <resources> <resource> <directory>res</directory> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> <source>1.8</source> <target>1.9</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <outputDirectory>lib</outputDirectory> </configuration> </plugin> </plugins> </build> <dependencies> <!-- ====================== 运行依赖 ========================== --> <!-- https://mvnrepository.com/artifact/org.apache.ant/ant --> <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.9.10</version> </dependency> <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j --> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml/jaxrpc-api --> <dependency> <groupId>javax.xml</groupId> <artifactId>jaxrpc-api</artifactId> <version>1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.eclipse.jdt.core.compiler/ecj --> <dependency> <groupId>org.eclipse.jdt.core.compiler</groupId> <artifactId>ecj</artifactId> <version>4.6.1</version> </dependency> <!-- ====================== 测试依赖 ========================== --> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.easymock/easymock --> <dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> <version>3.4</version> <scope>test</scope> </dependency> <!-- 修复jsp无法打开问题 --> <!-- 其实就是编译时,文件res/META-INF/jasper.jar/services/javax.servlet.ServletContainerInitializer位置没放对,要放在classpath:/META-INF /services/javax.servlet.ServletContainerInitializer --> <!-- 与其增加配置量,直接引入jar包算了 --> <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jasper</artifactId> <version>8.5.81</version> </dependency> </dependencies> </project>
-
导入maven工程
-
添加 ${tomcat.src}/java 文件夹为编译目录
-
设置启动选项,并启动
org.apache.catalina.startup.Bootstrap
的 main 方法
-Dcatalina.home=${project_loc} -Dcatalina.base=${project_loc} -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=${project_loc}/conf/logging.properties -Duser.language=en
💡 提示
如果显示乱码
解决:升级 jdk 到8以上(不包括8),或者添加
-Duser.language=en
启动参数原因:
这篇文章,对tomcat乱码问题有非常全面的研究: https://juejin.cn/post/6967701837696729124
总结来说就是: tomcat 源码文件全是 utf8 编码的
但是,可能是用的jdk8及一下版本处理properties文件用 ISO-8859-1 编码
-
访问 8080 端口
💡 提示
如果报错:
java.lang.NullPointerException: Cannot invoke "javax.servlet.jsp.JspFactory.releasePageContext(javax.servlet.jsp.PageContext)" because "org.apache.jsp.index_jsp._jspxFactory" is null
或者java.lang.NullPointerException: Cannot invoke "javax.servlet.jsp.JspFactory.releasePageContext(javax.servlet.jsp.PageContext)" because "org.apache.jsp.index_jsp._jspxFactory" is null
则需要添加以下依赖:<!-- 修复jsp无法打开问题 --> <!-- 其实就是编译时,文件res/META-INF/jasper.jar/services/javax.servlet.ServletContainerInitializer位置没放对,要放在classpath:/META-INF/services/javax.servlet.ServletContainerInitializer --> <!-- 与其增加配置量,直接引入jar包算了 --> <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jasper</artifactId> <version>8.5.81</version> </dependency>
或者不添加依赖,到
ContextConfig
类中configureStart
这个函数的webConfig();
后手动将jsp解析器初始化既可