zoukankan      html  css  js  c++  java
  • Tomcat:源码环境搭建

    下载tomcat源码

    来到tomcat官网:https://tomcat.apache.org/

    我这里下载tomcat9的代码,点击download

    image-20211028101232829

    下载zip包:

    image-20211028101303627

    下载完成后,解压后,新建pom.xml文件放到源码根目录下:

    image-20211028101409814

    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>tomcat9</artifactId>
        <name>tomcat-9.0.54</name>
        <version>9.0.54</version>
        <build>
            <finalName>tomcat-9.0.54</finalName>
            <sourceDirectory>java</sourceDirectory>
            <!--<testSourceDirectory>test</testSourceDirectory>  test 下的有些文件报错,因此将test文件夹去掉了-->
            <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>3.6.0</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
            </plugins>
        </build>
         <dependencies>
            <dependency>
                <groupId>org.apache.ant</groupId>
                <artifactId>ant</artifactId>
                <version>1.9.5</version>
            </dependency>
             <!-- https://mvnrepository.com/artifact/biz.aQute.bnd/annotation -->
             <!-- https://mvnrepository.com/artifact/biz.aQute.bnd/biz.aQute.bndlib -->
             <dependency>
                 <groupId>biz.aQute.bnd</groupId>
                 <artifactId>biz.aQute.bndlib</artifactId>
                 <version>5.2.0</version>
                 <scope>provided</scope>
             </dependency>
    
             <!-- https://mvnrepository.com/artifact/org.apache.jasper/org.apache.jasper -->
             <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
             <dependency>
                 <groupId>org.apache.tomcat</groupId>
                 <artifactId>tomcat-jasper</artifactId>
                 <version>9.0.54</version>
             </dependency>
             <dependency>
                <groupId>org.apache.ant</groupId>
                <artifactId>ant-apache-log4j</artifactId>
                <version>1.9.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.ant</groupId>
                <artifactId>ant-commons-logging</artifactId>
                <version>1.9.5</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.rpc</groupId>
                <artifactId>javax.xml.rpc-api</artifactId>
                <version>1.1</version>
            </dependency>
            <dependency>
                <groupId>wsdl4j</groupId>
                <artifactId>wsdl4j</artifactId>
                <version>1.6.2</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.jdt.core.compiler</groupId>
                <artifactId>ecj</artifactId>
                <version>4.6.1</version>
            </dependency>
        </dependencies>
    </project>
    

    找到主启动类

    直接导入到idea中,等待下载依赖完成。

    导入后,那么怎么找到主启动类?

    一般来说,我们在windows启动tomcat,是直接运行startup.bat文件的.

    那我们先看startup.bat文件,这里主要运行catalina.bat

    image-20211028102030715

    再看catalina.bat文件,这里面有一段比较重要的,执行call调用了另外一个setclasspath.bat

    image-20211028102149765

    这里setclasspath.bat就设置了java命令的路径,在_RUNJAVA变量中

    image-20211028102326868

    再返回到catalina.bat脚本中,设置了_EXECJAVA=%_RUNJAVA%

    image-20211028102431040

    再到最后执行java命令启动tomcat:

    rem Execute Java with the applicable properties
    if not "%JPDA%" == "" goto doJpda
    if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
    %_EXECJAVA% %CATALINA_LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
    goto end
    :doSecurity
    %_EXECJAVA% %CATALINA_LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
    goto end
    :doJpda
    if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
    %_EXECJAVA% %CATALINA_LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
    goto end
    :doSecurityJpda
    %_EXECJAVA% %CATALINA_LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
    goto end
    

    里面有重要的一个参数,MAINCLASS,那么MAINCLASS就可能是主启动类,

    MAINCLASS前面声明为set MAINCLASS=org.apache.catalina.startup.Bootstrap

    我们去找org.apache.catalina.startup.Bootstrap这个类,这个类中果然有main方法。

    设置idea,使用jdk版本是1.8

    image-20211028102957760

    直接运行Bootstrap类:运行成功,但是有部分乱码。

    image-20211028103104921

    解决乱码问题

    1.找到org.apache.tomcat.util.res.StringManager类的getString方法,加入如下代码:

            try {
                str = new String(str.getBytes("ISO-8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
    
    image-20211028103536802

    2.找到org.apache.jasper.compiler.Localizer类的getMessage方法,添加同样的代码:

    image-20211028103719613

    重新启动主方法,已经不乱码了。

    image-20211028103759506

    同时我们访问localhost:8080,也是可以访问的。

    image-20211028103834022
  • 相关阅读:
    Windows 配置JAVA的环境变量
    不直接用NSLog
    Mongodb for Mac 安装
    Redis Mac 安装及简单命令使用
    第十五天和十六天学习笔记
    第十四天学习笔记
    第十三天学习笔记
    第十二天学习笔记
    第十一天学习笔记
    第十天学习笔记
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/15474459.html
Copyright © 2011-2022 走看看