一、下载源码
地址:https://tomcat.apache.org/download-90.cgi
二、运行
(1)解压zip压缩包
(2)进入解压目录,并创建一个目录,命名为home,并将conf、webapps目录移入home 目录中
(3)在当前目录下创建一个 pom.xml 文件,引入tomcat的依赖包
<?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>Tomcat9.0</artifactId>
<name>Tomcat9</name>
<version>9.0.37</version>
<build>
<finalName>Tomcat9</finalName>
<sourceDirectory>java</sourceDirectory>
<resources>
<resource>
<directory>java</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-apache-log4j</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-commons-logging</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>javax.xml.rpc</groupId>
<artifactId>javax.xml.rpc-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</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>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(4)在idea中, 导入该工程
(5) 配置idea的启动类,配置 MainClass,并配置 VM 参数。
-Dcatalina.home=D:\apache-tomcat-9.0.37-src\home
-Dcatalina.base=D:\apache-tomcat-9.0.37-src\home
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=D:\apache-tomcat-9.0.37-src\home\conf\logging.properties
(6)启动主方法, 运行Tomcat ,访问Tomcat 。
三、存在的问题
(1)控制台乱码
出现类似的乱码。
解决办法1:找到java/org/apache/tomcat/util/res/StringManager.java下的
private StringManager(String packageName, Locale locale)
改动如图所示
但是浏览器访问localhost:8080仍然出现乱码
解决办法2:找到java/org/apache/tomcat/util/res/StringManager.java下的
public String getString(final String key, final Object... args)
加入框中代码,改动如图所示:
找到java/org/apache/jasper/compiler/Localizer.java下的
public static String getMessage(String errCode)
加入框中代码,改动如图所示:
(2)浏览器报错
原因:出现上述异常的原因,是我们直接启动org.apache.catalina.startup.Bootstrap的时候没有加载JasperInitializer,从而无法编译JSP。解决办法是在tomcat的源码ContextConfig中的configureStart函数中手动将JSP解析器初始化:
解决办法:在 java/org/apache/catalina/startup/ContextConfig.java下
protected synchronized void configureStart()
加入
context.addServletContainerInitializer(new JasperInitializer(), null);
(7) 重启tomcat就可以正常访问了。