Tomcat源码运行
下载Tomcat源码并解压
下载地址:http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.59/src/apache-tomcat-8.5.59-src.zip
浏览器打开,或者终端直接执行
1
wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.59/src/apache-tomcat-8.5.59-src.zip
解压到想要存放的目录:
/home/jievhaha/Documents/workspace/ideaworkspace/apacheTomcat
解压得到:
Idea导入源码并配置
在源码根目录创建pom.xml(如上边截图)并添加如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<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.5</artifactId>
<name>Tomcat8.5 src</name>
<version>8.5</version>
<build>
<finalName>Tomcat8.5</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>3.5</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<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.5.1</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>Idea导入项目并配置参数:
- 导入项目后Run –> Edit Configurations
点击”+“,添加Application
Name随意,然后配置Configuration –> Main class,添加如下内容。
1
org.apache.catalina.startup.Bootstrap
配置VM options,添加如下内容(路径自己看着改,没有的文件夹可以自己创建,不影响)。
1
2
3
4
5
6
7
8-Dcatalina.home=/home/jievhaha/Documents/workspace/ideaworkspace/apacheTomcat
-Dcatalina.base=/home/jievhaha/Documents/workspace/ideaworkspace/apacheTomcat
-Djava.endorsed.dirs=/home/jievhaha/Documents/workspace/ideaworkspace/apacheTomcat/endorsed
-Djava.io.tmpdir=/home/jievhaha/Documents/workspace/ideaworkspace/apacheTomcattemp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=/home/jievhaha/Documents/workspace/ideaworkspace/apacheTomcat/conf/logging.properties
-Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-Djdk.tls.ephemeralDHKeySize=2048
打开mvn compile编译项目:
启动项目,访问localhost:8080
常见问题
启动Tomcat,Idea控制台乱码问题
具体原因记得是某个配置文件idea打开就是乱码,之后需要重新查看
修改tomcat源代码,在代码中获取到了值之后手动重新编码解码,添加如下内容:
1
2
3
4
5
6
7try {
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
}catch(Exception e){
e.printStackTrace();
}第一处:org.apache.tomcat.util.res.StringManager类中的getString(final String key, final Object... args)方法。
第二处:org.apache.jasper.compiler.Localizer类的getMessage(String errCode)方法。
启动失败,打开localhost:8080报错,提示:无法为JSP编译类
解决办法:org.apache.catalina.startup.ContextConfig.java文件中protected synchronized void configureStart() {}中webConfig();下添加
1
context.addServletContainerInitializer(new JasperInitializer(), null);