Tomcat 是咱們每一個 Java 工做者都會接觸的,咱們每天接觸它,使用它,可是不少人就僅僅只知道如何使用它,它的工做原理是如何的?啓動過程是如何的?不多有人去了解、分析它,已經要對Tomcat進行配置優化,因此瞭解他的工做原理仍是頗有必要的。java
將下載下來的源碼壓縮包解壓,而後在解壓後的目錄中新建catalina-home目錄和pom.xml文件,將跟目錄中的conf和webapps文件夾複製到catalina-home目錄中,以下
web
pom文件內容以下apache
<?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</artifactId> <name>Tomcat8</name> <version>8</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> <dependency> <groupId>javax.xml.soap</groupId> <artifactId>javax.xml.soap-api</artifactId> <version>1.4.0</version> </dependency> </dependencies> </project>
catalina-home目錄結構以下,沒有的目錄結構新建空文件夾api
1.使用IDEA 以maven的項目導入瀏覽器
2.構建好項目後配置啓動參數tomcat
Main class設置爲org.apache.catalina.startup.Bootstrapapp
-Dcatalina.home=C:/develop/code/apache-tomcat-8.5.35-src/catalina-home
-Dcatalina.base=C:/develop/code/apache-tomcat-8.5.35-src/catalina-home
-Djava.endorsed.dirs=C:/develop/code/apache-tomcat-8.5.35-src/catalina-home/endorsed
-Djava.io.tmpdir=C:/develop/code/apache-tomcat-8.5.35-src/catalina-home/temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=C:/develop/code/apache-tomcat-8.5.35-src/catalina-home/conf/logging.propertieseclipse
說明:若是編譯build的時候出現Test測試代碼報錯,註釋該代碼便可。本文中的Tomcat源碼util.TestCookieFilter類會報錯,將其註釋便可,catalina-home目錄用全路徑webapp
1.在org.apache.catalina.startup.ContextConfig類中手動將JSP解析器初始化maven
context.addServletContainerInitializer(new JasperInitializer(), null);
2.修改完後,點擊運行或者調試按鈕後,正常運行。
訪問瀏覽器 http://localhost:8080/