记录下IDEA中调试Tomcat源码的过程

打算动态调试看看Tomcat源码, 发现Tomcat是使用ant构建的, 直接复制源码到IDEA中不太方便,研究了下怎么在IDEA中动态调试Tomcat源码

  1. 下载 Tomcat源码, 解压到D盘根目录
  2. 安装 Ant

    1. 下载 Ant 二进制包, 解压到D盘根目录
    2. 配置环境变量 ANT_HOME = D:\apache-ant-1.10.3PATH中追加;%ANT_HOME%\bin
  3. 编译 Tomcat

    D:
    cd D:/apache-tomcat-8.5.29-src
    ant
    
  4. IDEA中新建个serlvet+jsp的项目

    IndexServlet.java

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/")
    public class IndexServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().print("Hello World");
        }
    }
    

    EchoServlet.java

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/echo")
    public class EchoServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().print("echo");
        }
    }
    

    hello.jsp

    <%--
      Created by IntelliJ IDEA.
      User: ilanyu
      Date: 2018/4/9
      Time: 10:53
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    hello.jsp
    </body>
    </html>
    
  5. 生成war包,修改war包名字为ROOT.war
  6. IDEA中新建J2SE项目

    com/lanyus/Main.java

    package com.lanyus;
    
    public class Main {
        public static void main(String[] args) {
            org.apache.catalina.startup.Bootstrap.main(args);
        }
    }
    
  7. D:\apache-tomcat-8.5.29-src\output\buildbinconflib 复制到项目中,将 binbootstrap.jarcommons-daemon.jartomcat-juli.jar 复制到 lib 中,在 lib 上右键, 选择 Add As Library...
  8. D:\apache-tomcat-8.5.29-src\java 复制到项目中, 将第4步新建的文件复制到java目录中
  9. 在项目中新建 webapps,将第4步得到的ROOT.war复制到webapps中
  10. 单步运行Main类,进入org.apache.catalina.startup.Bootstrap.main后选择源码目录,选择 java 目录

最终效果如图

推荐几篇感觉写得还不错的分析Tomcat的文章

https://blog.csdn.net/cx520forever/article/details/52743166

https://www.cnblogs.com/zhouyuqin/p/5143121.html

标签: none

添加新评论