个人随笔
目录
Tomcat篇:Tomcat源码Eclipse环境搭建(二)
2022-02-26 15:32:38

前言

最近开始研究下Tomcat源码,毕竟用了这么多年了,却没有深入了解它,最多网上看看博文帖子,知道它由什么Server、Service、Engine、Host、Context、Wrapper等组件,却完全不知道,真正的数据流动,比如怎么解析浏览器请求,怎么到Servlet的,所以考虑搭建下源码环境,仔细debug下。

下载源码

这个只需要访问官网
https://tomcat.apache.org/

然后点击左边的Download,这里下载的是Tomcat8,版本为8.5.75,进入到Tomcat8的页面https://tomcat.apache.org/download-80.cgi

点击最下面的Source Code Distributions-zip,具体链接为
https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.75/src/apache-tomcat-8.5.75-src.zip

还有eclipse,jdk,maven等这些必备的工具环境,话说网上有人说应为tomcat是用ant构建的,所以也要下载ant,然后进入源码目录里面cmd执行ant,可惜我这里不知怎么回事,一直报如下错误,可能是网络不行。

所以还是采取maven的方式

导入源码

我们在源码目录里面新建pom.xml文件,内如如下(改为自己的tomcat版本)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>org.apache.tomcat</groupId>
  8. <artifactId>apache-tomcat-8.5.75-src</artifactId>
  9. <name>Tomcat8.5.75</name>
  10. <version>8.5.75</version>
  11. <build>
  12. <!--指定源⽬录 -->
  13. <finalName>Tomcat8.5.75</finalName>
  14. <sourceDirectory>java</sourceDirectory>
  15. <resources>
  16. <resource>
  17. <directory>java</directory>
  18. </resource>
  19. </resources>
  20. <plugins>
  21. <!--引⼊编译插件 -->
  22. <plugin>
  23. <groupId>org.apache.maven.plugins</groupId>
  24. <artifactId>maven-compiler-plugin</artifactId>
  25. <version>3.1</version>
  26. <configuration>
  27. <encoding>UTF-8</encoding>
  28. <source>8</source>
  29. <target>8</target>
  30. </configuration>
  31. </plugin>
  32. </plugins>
  33. </build>
  34. <!--tomcat 依赖的基础包 -->
  35. <dependencies>
  36. <dependency>
  37. <groupId>org.easymock</groupId>
  38. <artifactId>easymock</artifactId>
  39. <version>3.4</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>ant</groupId>
  43. <artifactId>ant</artifactId>
  44. <version>1.7.0</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>wsdl4j</groupId>
  48. <artifactId>wsdl4j</artifactId>
  49. <version>1.6.2</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>javax.xml</groupId>
  53. <artifactId>jaxrpc</artifactId>
  54. <version>1.1</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.eclipse.jdt.core.compiler</groupId>
  58. <artifactId>ecj</artifactId>
  59. <version>4.5.1</version>
  60. </dependency>
  61. <dependency>
  62. <groupId>javax.xml.soap</groupId>
  63. <artifactId>javax.xml.soap-api</artifactId>
  64. <version>1.4.0</version>
  65. </dependency>
  66. </dependencies>
  67. </project>

然后用eclipse导入已存在的maven项目就可以了

启动测试

Tomcat的启动类是org.apache.catalina.startup.Bootstrap,只有这个类有个main方法,右键配置Debug Configurations,配置Arguments下面的VM arguments添加如下配置

  1. -Dcatalina.home=F:/Source/apache-tomcat-8.5.75-src
  2. -Dcatalina.base=F:/Source/apache-tomcat-8.5.75-src
  3. -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
  4. -Djava.util.logging.config.file=F:/Source/apache-tomcat-8.5.75-src/conf/logging.properties

然后右键debug启动,发现启动成功

  1. 26-Feb-2022 15:07:02.364 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory
  2. 26-Feb-2022 15:07:02.442 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 2810 ms

浏览器访问http://localhost:8080/,发现乱码了

看错误是JSP解析引擎未初始化,我们去org.apache.catalina.startup.ContextConfig的configureStart方法webConfig();后面加上

  1. //初始化JSP解析引擎
  2. context.addServletContainerInitializer(new JasperInitializer(),null);
  1. protected synchronized void configureStart() {
  2. // Called from StandardContext.start()
  3. if (log.isDebugEnabled()) {
  4. log.debug(sm.getString("contextConfig.start"));
  5. }
  6. if (log.isDebugEnabled()) {
  7. log.debug(sm.getString("contextConfig.xmlSettings",
  8. context.getName(),
  9. Boolean.valueOf(context.getXmlValidation()),
  10. Boolean.valueOf(context.getXmlNamespaceAware())));
  11. }
  12. webConfig();
  13. //初始化JSP解析引擎
  14. context.addServletContainerInitializer(new JasperInitializer(),null);
  15. if (!context.getIgnoreAnnotations()) {
  16. applicationAnnotationsConfig();
  17. }
  18. if (ok) {
  19. validateSecurityRoles();
  20. }
  21. // Configure an authenticator if we need one
  22. if (ok) {
  23. authenticatorConfig();
  24. }
  25. // Dump the contents of this pipeline if requested
  26. if (log.isDebugEnabled()) {
  27. log.debug("Pipeline Configuration:");
  28. Pipeline pipeline = context.getPipeline();
  29. Valve valves[] = null;
  30. if (pipeline != null) {
  31. valves = pipeline.getValves();
  32. }
  33. if (valves != null) {
  34. for (Valve valve : valves) {
  35. log.debug(" " + valve.getClass().getName());
  36. }
  37. }
  38. log.debug("======================");
  39. }
  40. // Make our application available if no problems were encountered
  41. if (ok) {
  42. context.setConfigured(true);
  43. } else {
  44. log.error(sm.getString("contextConfig.unavailable"));
  45. context.setConfigured(false);
  46. }
  47. }

再启动访问就完美了

 194

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2