idea调试tomcat源码

优化Tomcat AJP 误报时有一些疑问,调试一下

1 下载tomcat

历史版本列表:https://archive.apache.org/dist/tomcat/tomcat-8/
源码文件夹:https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.50/src/
下载apache-tomcat-8.5.50-src.zip
解压一下

2 导入pom.xml

在源码根目录下新建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.0</artifactId>
    <name>Tomcat8.0</name>
    <version>8.0</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.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>

3 idea打开/配置/运行

(1 idea打开源码路径
(2 配置JDK: File -> Project Structure
image.png
(3 新建configurations
新增application,并指向tomcat 入口 "Bootstrap"
image.png

4 运行解决报错

(1 报错一 测试类报错

/apache-tomcat-8.5.50-src/test/util/TestCookieFilter.java:29:36
java: 找不到符号
  符号:   变量 CookieFilter
  位置: 类 util.TestCookieFilter

解决:
注释整个/apache-tomcat-8.5.50-src/test/util/TestCookieFilter.java文件

(2 报错二 解析报错

Type Exception Report

Message java.lang.NullPointerException

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: java.lang.NullPointerException
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:598)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:514)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
Root Cause

java.lang.NullPointerException
	org.apache.jsp.index_jsp._jspService(index_jsp.java:424)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
Note The full stack trace of the root cause is available in the server logs.

image.png

解决:
位置:

  • 文件:/apache/catalina/startup/ContextConfig.java
  • 类:protected synchronized void configureStart()
  • 具体位置:webConfig(); 下方

image.png
添加代码

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

并添加引用
image.png

5 正常运行

image.png
image.png

参考链接
pom.xml 出处:https://blog.csdn.net/wangjunjie0817/article/details/102944338
报错2处理:https://www.jianshu.com/p/3b6b393062cc
搭建:https://blog.csdn.net/Dyoungwhite/article/details/122085305

posted @ 2022-08-22 20:42  huim  阅读(810)  评论(0编辑  收藏  举报