源码下载(Source Code Distributions)地址:https://tomcat.apache.org/download-90.cgi

tomcat 和 servlet 以及 jdk 版本的对应关系:http://tomcat.apache.org/whichversion.html

附上搭建好的环境:https://gitee.com/jhxxb/MyTomcat

Maven方式

一、解压源码

在源码根目录新建 home 文件夹,把 conf 文件夹和 webapps 文件夹移动到 home 文件夹里,然后在源码根目录新建 pom.xml 文件(原来为 Ant 工程,这里把它改为 Maven 工程)

pom.xml

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.  
  6. <modelVersion>4.0.0</modelVersion>
  7.  
  8. <groupId>org.apache.tomcat</groupId>
  9. <artifactId>tomcat</artifactId>
  10. <name>tomcat</name>
  11. <version>9.0.19</version>
  12.  
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>4.12</version>
  18. <scope>test</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.apache.ant</groupId>
  22. <artifactId>ant</artifactId>
  23. <version>1.10.5</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>wsdl4j</groupId>
  27. <artifactId>wsdl4j</artifactId>
  28. <version>1.6.3</version>
  29. </dependency>
  30.  
  31. <!--<dependency>-->
  32. <!--<groupId>javax.xml</groupId>-->
  33. <!--<artifactId>jaxrpc</artifactId>-->
  34. <!--<version>1.1</version>-->
  35. <!--</dependency>-->
  36. <dependency>
  37. <groupId>org.apache.geronimo.specs</groupId>
  38. <artifactId>geronimo-jaxrpc_1.1_spec</artifactId>
  39. <version>2.1</version>
  40. </dependency>
  41.  
  42. <!-- <dependency>-->
  43. <!-- <groupId>org.eclipse.jdt.core.compiler</groupId>-->
  44. <!-- <artifactId>ecj</artifactId>-->
  45. <!-- <version>4.5</version>-->
  46. <!-- </dependency>-->
  47. <dependency>
  48. <groupId>org.eclipse.jdt</groupId>
  49. <artifactId>ecj</artifactId>
  50. <version>3.17.0</version>
  51. </dependency>
  52.  
  53. <dependency>
  54. <groupId>org.easymock</groupId>
  55. <artifactId>easymock</artifactId>
  56. <version>4.0.2</version>
  57. <scope>test</scope>
  58. </dependency>
  59. </dependencies>
  60.  
  61. <build>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.apache.maven.plugins</groupId>
  65. <artifactId>maven-compiler-plugin</artifactId>
  66. <configuration>
  67. <source>1.8</source>
  68. <target>1.8</target>
  69. <encoding>UTF-8</encoding>
  70. </configuration>
  71. </plugin>
  72. <plugin>
  73. <groupId>org.apache.maven.plugins</groupId>
  74. <artifactId>maven-resources-plugin</artifactId>
  75. <configuration>
  76. <encoding>UTF-8</encoding>
  77. </configuration>
  78. </plugin>
  79. </plugins>
  80. </build>
  81. </project>

二、用 IDEA 直接打开

这里删除了一些无用的文件

1.打开项目属性(F4)

把 java 文件夹标记为 Sources,test 文件夹标记为 Tests

2.运行

找到 org.apache.catalina.startup.Bootstrap 运行 main 方法

2.1、ResponseTrailers 找不到,把 home\webapps\examples\WEB-INF\classes\trailers 目录拷贝到 test 目录下

ResponseTrailers.java

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package trailers;
  18.  
  19. import java.io.IOException;
  20. import java.io.PrintWriter;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.function.Supplier;
  24.  
  25. import javax.servlet.ServletException;
  26. import javax.servlet.http.HttpServlet;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29.  
  30. /**
  31. * This example writes some trailer fields to the HTTP response.
  32. */
  33. public class ResponseTrailers extends HttpServlet {
  34.  
  35. private static final long serialVersionUID = 1L;
  36. private static final Supplier<Map<String,String>> TRAILER_FIELD_SUPPLIER =
  37. new TrailerFieldSupplier();
  38.  
  39. @Override
  40. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  41. throws ServletException, IOException {
  42.  
  43. resp.setTrailerFields(TRAILER_FIELD_SUPPLIER);
  44. resp.setContentType("text/plain");
  45. resp.setCharacterEncoding("UTF-8");
  46.  
  47. PrintWriter pw = resp.getWriter();
  48.  
  49. pw.print("This response should include trailer fields.");
  50. }
  51.  
  52. private static class TrailerFieldSupplier implements Supplier<Map<String,String>> {
  53.  
  54. private static final Map<String,String> trailerFields = new HashMap<>();
  55.  
  56. static {
  57. trailerFields.put("x-trailer-1", "Trailer value one");
  58. trailerFields.put("x-trailer-2", "Trailer value two");
  59. }
  60.  
  61. @Override
  62. public Map<String, String> get() {
  63. return trailerFields;
  64. }
  65. }
  66. }

2.2、CookieFilter 找不到,把 home\webapps\examples\WEB-INF\classes\util\CookieFilter.java 文件拷贝到 test\util 目录下

CookieFilter.java

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package util;
  18.  
  19. import java.util.Locale;
  20. import java.util.StringTokenizer;
  21.  
  22. /**
  23. * Processes a cookie header and attempts to obfuscate any cookie values that
  24. * represent session IDs from other web applications. Since session cookie names
  25. * are configurable, as are session ID lengths, this filter is not expected to
  26. * be 100% effective.
  27. *
  28. * It is required that the examples web application is removed in security
  29. * conscious environments as documented in the Security How-To. This filter is
  30. * intended to reduce the impact of failing to follow that advice. A failure by
  31. * this filter to obfuscate a session ID or similar value is not a security
  32. * vulnerability. In such instances the vulnerability is the failure to remove
  33. * the examples web application.
  34. */
  35. public class CookieFilter {
  36.  
  37. private static final String OBFUSCATED = "[obfuscated]";
  38.  
  39. private CookieFilter() {
  40. // Hide default constructor
  41. }
  42.  
  43. public static String filter(String cookieHeader, String sessionId) {
  44.  
  45. StringBuilder sb = new StringBuilder(cookieHeader.length());
  46.  
  47. // Cookie name value pairs are ';' separated.
  48. // Session IDs don't use ; in the value so don't worry about quoted
  49. // values that contain ;
  50. StringTokenizer st = new StringTokenizer(cookieHeader, ";");
  51.  
  52. boolean first = true;
  53. while (st.hasMoreTokens()) {
  54. if (first) {
  55. first = false;
  56. } else {
  57. sb.append(';');
  58. }
  59. sb.append(filterNameValuePair(st.nextToken(), sessionId));
  60. }
  61.  
  62. return sb.toString();
  63. }
  64.  
  65. private static String filterNameValuePair(String input, String sessionId) {
  66. int i = input.indexOf('=');
  67. if (i == -1) {
  68. return input;
  69. }
  70. String name = input.substring(0, i);
  71. String value = input.substring(i + 1, input.length());
  72.  
  73. return name + "=" + filter(name, value, sessionId);
  74. }
  75.  
  76. public static String filter(String cookieName, String cookieValue, String sessionId) {
  77. if (cookieName.toLowerCase(Locale.ENGLISH).contains("jsessionid") &&
  78. (sessionId == null || !cookieValue.contains(sessionId))) {
  79. cookieValue = OBFUSCATED;
  80. }
  81.  
  82. return cookieValue;
  83. }
  84. }

2.3、conf\server.xml 找不到,设置下 jvm 参数(就是指定之前创建的 home 目录)

  1. -Dcatalina.home=D:\CodeLib\tomcat9\home

2.4、控制台报错:Error configuring application listener of class [listeners.ContextListener]

没找到具体原因,删掉 webapps 下的 examples 文件夹即可

2.5、控制台或网页报错:Servlet.service() for servlet [jsp] in context with path [] threw exception [org.apache.jasper.JasperException: Unable to compile class for JSP] with root cause

编辑 org.apache.catalina.startup.ContextConfig 文件的 configureStart() 方法,添加初始化 JSP 解析器的代码

  1. context.addServletContainerInitializer(new JasperInitializer(), null);

最后访问 http://127.0.0.1:8080/ 就可以看到欢迎页了

Ant方式

解压 tomcat 源码,编辑 build.properties.default 文件,修改 base.path 路径

  1. # ----- Default Base Path for Dependent Packages -----
  2. # Please note this path must be absolute, not relative,
  3. # as it is referenced with different working directory
  4. # contexts by the various build scripts.
  5. base.path=D:/CodeLib/apache-tomcat-9.0.19-src/tomcat-build-libs

下载 Ant,配置环境变量

https://ant.apache.org/bindownload.cgi

  1. # 设置系统环境变量
  2. setx /M ANT_HOME "D:\apache-ant-1.10.5"
  3. setx /M Path "%Path%;%ANT_HOME%\bin"

编译 tomcat,生成 idea 项目

  1. cd D:\CodeLib\apache-tomcat-9.0.19-src
  2. ant
  3. ant ide-intellij

用 idea 打开源码目录,会提示设置变量属性,按照自己设置的目录设置即可,后面运行和 Maven 方式类似


https://gongxufan.github.io/2017/10/20/tomcat-source-debug/

https://emacsist.github.io/tags/tomcat%E6%BA%90%E7%A0%81/

IDEA 导入 Tomcat9 源码的更多相关文章

  1. IDEA导入tomcat9源码跑起来~

    如题,这里记录一下用IDEA导入tomcat9的源码,并跑起来.看了本教程你还是不会的话直接问我. 一.环境安装以及目录搭建 tomcat9源码下载地址:http://mirrors.hust.edu ...

  2. 探秘Tomcat(一)——Myeclipse中导入Tomcat源码

    前言:有的时候自己不知道自己是井底之蛙,这并没有什么可怕的,因为你只要蜷缩在方寸之间的井里,无数次的生活轨迹无非最终归结还是一个圆形:但是可怕的是有一天你不得不从井里跳出来生活,需要重新审视井以外的生 ...

  3. Eclipse导入Hadoop源码项目及编写Hadoop程序

    一 Eclipse导入Hadoop源码项目 基本步骤: 1)在Eclipse新建一个java项目[hadoop-1.2.1] 2)将Hadoop压缩包解压目录src下的core,hdfs,mapred ...

  4. 关于导入geoserver 源码到Eclipse编译运行

    参考http://blog.csdn.net/gisshixisheng/article/details/43016443 和  http://blog.sina.com.cn/s/blog_6e37 ...

  5. spring源码学习(一):eclipse导入spring源码

    前言 对于一门技术,我们最先是了解它(what),然后再熟练的使用它(how)以及何时用它(when),最后肯定要看透它(why).spring作为Java开发人员可以说是最熟悉不过的了,基本每个Ja ...

  6. eclipse导入Java源码

    eclipse导入Java源码 下载源码包(一般jdk都自带了, 我的没有) src.zip eclipse -> window -> preferences -> JAVA -&g ...

  7. Hadoop1.x目录结构及Eclipse导入Hadoop源码项目

    这是解压hadoop后,hadoop-1.2.1目录 各目录结构及说明: Eclipse导入Hadoop源码项目: 注意:如果没有ant的包可以去网上下,不是hadoop里面的. 然后如果通过以上还报 ...

  8. idea 导入spring 源码注意的问题

    问题:idea导入spring 源码的步骤是: 首先从官网下载spring的源码:git clone https://github.com/spring-projects/spring-framewo ...

  9. 导入android源码中的APP源码到eclipse

    导入android源码中的APP源码到eclipse 一般最简单的办法就是创建新的android工程,选择create project from existing source选项,直接导入源码就OK ...

随机推荐

  1. 虹软免费人脸识别SDK注册指南

    成为开发者三步完成账号的基本注册与认证:STEP1:点击注册虹软AI开放平台右上角注册选项,完成注册流程.STEP2:首次使用,登录后进入开发者中心,点击账号管理完成企业或者个人认证,若未进行实名认证 ...

  2. asp.net core 集成 log4net 日志框架

    asp.net core 集成 log4net 日志框架 Intro 在 asp.net core 中有些日志我们可能想输出到数据库或文件或elasticsearch等,如果不自己去实现一个 Logg ...

  3. SQL常用语句(二)

    --语 句 功 能--数据操作SELECT --从数据库表中检索数据行和列INSERT --向数据库表添加新数据行DELETE --从数据库表中删除数据行UPDATE --更新数据库表中的数据 --数 ...

  4. Linux通过NFS实现文件共享

    在项目生产环境我们经常需要实现文件共享,传统的常见方案是通过NFS,实现服务器之间共享某一块磁盘,通过网络传输将分散的文件集中存储在一块指定的共享磁盘,实现基本的文件共享.实现这种方案,分服务端和客户 ...

  5. 搭建 structs2 环境

    前言 环境: window 10 ,JDK 1.8 ,Tomcat 7 ,MyEclipse 2014 pro 搭建 SSH 环境的步骤 创建 JavaWeb 项目 导入 structs2 的jar包 ...

  6. RC4

    RC4(Rivest Cipher 4)是一种流加密算法,密钥长度可变.并且因为加解密时使用的密钥相同,所以也为对称加密.加密过程和解密过程仅明密文的区别. 主要分为初始化 s 盒和伪随机密码生成组成 ...

  7. Golang代码实现HTTPs(HTTPS证书生成和部署)

    在win7下试试: 1.实现一个最简单的HTTPS Web Server // gohttps/2-https/server.go package main import ( "fmt&qu ...

  8. 在Bootstrap开发框架中使用Grid++报表

    之前在随笔<在Winform开发中使用Grid++报表>介绍了在Winform环境中使用Grid++报表控件,本篇随笔介绍在Bootstrap开发框架中使用Grid++报表,也就是Web环 ...

  9. VS2019 实用设置

    本文记录了 VS2019 预览版使用过程中的一些设置,这些设置也同样适用于 VS2017,我们可以根据个人的实际情况进行修改. 滚动条(Scroll Bar) 将滚动条设置为 map mode 后,则 ...

  10. AppiumDesktop录制脚本

    AppiumDesktop启动页面: 启动AppiumDesktop以后点击该页面右上角的Start New Session按钮,就会启动一个新的会话窗口(如下图),在这个窗口我们需要配置一些Desi ...