学习目标:
本文是转载的,只是学习记录下!
原文路径如下:https://www.cnblogs.com/wk-missQ1/p/13138720.html
运行tomcat源码,有助于理解tomcat原理,学习tomcat!
学习内容:
1、下载tomcat源码
2、运行tomcat
开始
1、下载tomcat
2、配置路径和文件
1)下载到一个文件夹下,如tomcatSource,apache-tomcat-8.5.68-src是tomcat解压文件,catalina-home是自己创建的,放置配置文件的
2)catalina-home文件夹下内容,标红的是直接从源码包复制过来的
3)tomcatSource文件夹下加入pom文件,内容如下:
pom:
<?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>gxf</groupId>
<artifactId>apache-tomcat-8</artifactId>
<name>apache-tomcat-8-source</name>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>apache-tomcat-8.5.68-src</module>
</modules>
</project>
注意:配置好modele,指向你的源码包名字
4)apache-tomcat-8.5.68-src包下加入pom文件
pom内容:
<?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.5.68</artifactId>
<name>Tomcat8.5.68</name>
<version>8.5</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>3.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.0</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.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
</dependency>
</dependencies>
</project>
5)导入到idea中如下:
6)配置启动类
其中vm参数如下:
-Dcatalina.home=catalina-home
-Dcatalina.base=catalina-home
-Djava.endorsed.dirs=catalina-home/endorsed
-Djava.io.tmpdir=catalina-home/temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=catalina-home/conf/logging.properties
7)test类有出现包找不到的,我是把他给屏蔽了
8)启动tomcat
在浏览器运行:http://localhost:8080/出现错误
修改:
因为我们直接启动org.apache.catalina.startup.Bootstrap的时候没有加载org.apache.jasper.servlet.JasperInitializer,从而无法编译JSP。解决办法是:
打开文件:java/org/apache/catalina/startup/ContextConfig.java,找到webConfig();
方法,在下面加上context.addServletContainerInitializer(new JasperInitializer(),null);
如下:
设置完毕:再次访问
9)日志乱码
打印日志乱码虽说不是大事,但是也比较恶心
原文参考:https://blog.csdn.net/zhoutaoping1992/article/details/104751705
修改两个地方,将默认编码ISO-8859-1变成UTF-8
第一个地方:修改StringManager类中getString
第二个地方,修改Localizer类中getMessage方法
try{
errMsg = new String(errMsg.getBytes("ISO-8859-1"),"UTF-8");
}catch (Exception e){
e.printStackTrace();
}