Tomcat8源码导入idea运行
1)源码下载地址
https://tomcat.apache.org/download-80.cgi#8.5.35
Source Code Distributions
2)下载zip包
https://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.57/src/apache-tomcat-8.5.57-src.zip
3)解压到本地
E:\apache-tomcat-8.5.57-src\apache-tomcat-8.5.57-src
4) apache-tomcat-8.5.35-src根目录下创建新建catalina-home目录
5) apache-tomcat-8.5.35-src根目录下创建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.5</artifactId>
<name>Tomcat8.5</name>
<version>8.5</version>
<build>
<finalName>Tomcat8.5</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.3</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<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.4</version>
</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.5.1</version>
</dependency>
</dependencies>
</project>
7) 将apache-tomcat-8.0.53-src目录中的conf和webapps文件夹复制到catalina-home目录中
8) idea导入tomcat项目,就跟导入普通maven项目一样导入即可
导入成功后加载pom.xml依赖
9) 配置idea运行参数
main class:org.apache.catalina.startup.Bootstrap
vm options:
-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
-Dfile.encoding=UTF-8
-Duser.language=en
-Duser.region=US
配置编码和语言是解决乱码问题
02-Sep-2020 22:02:51.659 严重 [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal ç±äºä¹åçé误ï¼Context[/examples]å¯å¨å¤±è´¥
02-Sep-2020 22:02:51.668 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Webåºç¨ç¨åºç®å½[E:\apache-tomcat-8.5.57-src\apache-tomcat-8.5.57-src\catalina-home\webapps\examples]çé¨ç½²å·²å¨[209]毫ç§åå®æ
02-Sep-2020 22:02:51.668 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory æweb åºç¨ç¨åºé¨ç½²å°ç®å½ [E:\apache-tomcat-8.5.57-src\apache-tomcat-8.5.57-src\catalina-home\webapps\host-manager]
02-Sep-2020 22:02:51.774 信息 [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars è³å°æä¸ä¸ªJAR被æ«æç¨äºTLDä½å°æªåå«TLDã 为æ¤è®°å½å¨å¯ç¨è°è¯æ¥å¿è®°å½ï¼ä»¥è·åå·²æ«æä½æªå¨å¶ä¸æ¾å°TLDçå®æ´JARå表ã å¨æ«ææé´è·³è¿ä¸éè¦çJARå¯ä»¥ç¼©çå¯å¨æ¶é´åJSPç¼è¯æ¶é´ã
02-Sep-2020 22:02:51.783 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Webåºç¨ç¨åºç®å½[E:\apache-tomcat-8.5.57-src\apache-tomcat-8.5.57-src\catalina-home\webapps\host-manager]çé¨ç½²å·²å¨[115]毫ç§åå®æ
02-Sep-2020 22:02:51.783 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory æweb åºç¨ç¨åºé¨ç½²å°ç®å½ [E:\apache-tomcat-8.5.57-src\apache-tomcat-8.5.57-src\catalina-home\webapps\manager]
02-Sep-2020 22:02:51.892 信息 [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars è³å°æä¸ä¸ªJAR被æ«æç¨äºTLDä½å°æªåå«TLDã 为æ¤è®°å½å¨å¯ç¨è°è¯æ¥å¿è®°å½ï¼ä»¥è·åå·²æ«æä½æªå¨å¶ä¸æ¾å°TLDçå®æ´JARå表ã å¨æ«ææé´è·³è¿ä¸éè¦çJARå¯ä»¥ç¼©çå¯å¨æ¶é´åJSPç¼è¯æ¶é´ã
02-Sep-2020 22:02:51.894 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Webåºç¨ç¨åºç®å½[E:\apache-tomcat-8.5.57-src\apache-tomcat-8.5.57-src\catalina-home\webapps\manager]çé¨ç½²å·²å¨[111]毫ç§åå®æ
02-Sep-2020 22:02:51.894 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory æweb åºç¨ç¨åºé¨ç½²å°ç®å½ [E:\apache-tomcat-8.5.57-src\apache-tomcat-8.5.57-src\catalina-home\webapps\ROOT]
02-Sep-2020 22:02:51.975 信息 [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars è³å°æä¸ä¸ªJAR被æ«æç¨äºTLDä½å°æªåå«TLDã 为æ¤è®°å½å¨å¯ç¨è°è¯æ¥å¿è®°å½ï¼ä»¥è·åå·²æ«æä½æªå¨å¶ä¸æ¾å°TLDçå®æ´JARå表ã å¨æ«ææé´è·³è¿ä¸éè¦çJARå¯ä»¥ç¼©çå¯å¨æ¶é´åJSPç¼è¯æ¶é´ã
02-Sep-2020 22:02:51.977 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Webåºç¨ç¨åºç®å½[E:\apache-tomcat-8.5.57-src\apache-tomcat-8.5.57-src\catalina-home\webapps\ROOT]çé¨ç½²å·²å¨[83]毫ç§åå®æ
02-Sep-2020 22:02:51.981 信息 [main] org.apache.coyote.AbstractProtocol.start å¼å§åè®®å¤çå¥æ["http-nio-8080"]
10) 编译build
util.TestCookieFilter 测试出现编译错误,将测试用例代码注释即可
11) 运行项目,访问 http://localhost:8080
页面出现错误
org.apache.jasper.JasperException: Unable to compile class for JSP
错误原因:
org.apache.catalina.startup.Bootstrap的时候没有加载org.apache.jasper.servlet.JasperInitializer,从而无法编译JSP
解决方法:
修改类:org.apache.catalina.startup.ContextConfig#configureStart 方法
增加代码:context.addServletContainerInitializer(new JasperInitializer(), null);
protected synchronized void configureStart() {
// Called from StandardContext.start()
if (log.isDebugEnabled()) {
log.debug(sm.getString("contextConfig.start"));
}
if (log.isDebugEnabled()) {
log.debug(sm.getString("contextConfig.xmlSettings",
context.getName(),
Boolean.valueOf(context.getXmlValidation()),
Boolean.valueOf(context.getXmlNamespaceAware())));
}
webConfig();
/**
* 支持jsp
*/
context.addServletContainerInitializer(new JasperInitializer(), null);
if (!context.getIgnoreAnnotations()) {
applicationAnnotationsConfig();
}
12)再次运行项目
修改代码:apache-tomcat-8.5.57-src\catalina-home\webapps\ROOT\index.jsp
<div id="navigation" class="curved container">
<span id="nav-home"><a href="${tomcatUrl}">Home(首页)</a></span>
<span id="nav-hosts"><a href="${tomcatDocUrl}">Documentation</a></span>
<span id="nav-config"><a href="${tomcatDocUrl}config/">Configuration</a></span>
<span id="nav-examples"><a href="${tomcatExamplesUrl}">Examples</a></span>
<span id="nav-wiki"><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></span>
<span id="nav-lists"><a href="${tomcatUrl}lists.html">Mailing Lists</a></span>
<span id="nav-help"><a href="${tomcatUrl}findhelp.html">Find Help</a></span>
<br class="separator" />
</div>
上一篇: idea 编译tomcat8源码
下一篇: Tomcat介绍及安装