Idea中構建Tomcat8源碼開發環境

在研究SpringMVC源碼以前得先看看Tomcat的源碼(由於WEB應用要工做在容器中)。先構建Tomcat8的源碼環境java

下載

進入Tomcat官網https://tomcat.apache.org/download-80.cgi,選擇tomcat8,下載下來是tomcat-8.5.49web

導入到Idea中

準備

新建一個tomcat-8.5.49目錄,再到其下新建一個deploy(是Tomcat的工做目錄:-Dcatalina.home=x:\\*.*\deploy -Dcatalina.base=x:\\*.*\deploy)目錄和導入源碼目錄,最終效果以下apache

deploy目錄下的結構,除了能夠複製的,其餘目錄直接新建tomcat

在tomcat-8.5.49目錄下新建一個pom.xmlcookie

內容以下:session

<?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>gxf</groupId>    
    <artifactId>apache-tomcat-8</artifactId>    
    <name>apache-tomcat-8</name>
    <version>1.0</version>    
    <packaging>pom</packaging> 
    <modules>    
        <module>tomcat-8</module>
    </modules>    
</project>

在tomcat-8目錄下新建pom.xml,文件內容以下:app

<?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>tomcat-8</artifactId>
    <name>tomcat-8</name>
    <version>8.5</version>


	<build>    
        <finalName>tomcat8.0</finalName>w
        <sourceDirectory>java</sourceDirectory>
        <!--CookieFilter 報錯的一種解決方法就是將測試目錄註釋掉-->
        <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>3.7.0</version>

                <configuration>    
                    <encoding>UTF-8</encoding>    
                    <source>1.8</source>    
                    <target>1.8</target>    
                </configuration>    
            </plugin>    
        </plugins>    
    </build>    

   <dependencies>  
        <dependency>  
            <groupId>org.easymock</groupId>  
            <artifactId>easymock</artifactId>  
            <version>3.5</version>  
            <scope>test</scope>  
        </dependency>  
  
        <dependency>    
            <groupId>junit</groupId>    
            <artifactId>junit</artifactId>    
            <version>4.12</version>  
            <scope>test</scope>    
        </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.6.1</version>  
        </dependency>    
    </dependencies>    
</project>

執行導入

File-Open-選擇tomcat-8.5.49目錄下的pom.xml文件,Idea會提示你,選擇導入爲Maven項目eclipse

導入成功以後以下所示:maven

中間會遇到的問題:ide

CookieFilter會報錯,有三種解決方式:

1.在test源碼目錄下的util目錄下新建CookieFitler

package util;

import java.util.Locale;
import java.util.StringTokenizer;

/**
 * Processes a cookie header and attempts to obfuscate any cookie values that
 * represent session IDs from other web applications. Since session cookie names
 * are configurable, as are session ID lengths, this filter is not expected to
 * be 100% effective.
 *
 * It is required that the examples web application is removed in security
 * conscious environments as documented in the Security How-To. This filter is
 * intended to reduce the impact of failing to follow that advice. A failure by
 * this filter to obfuscate a session ID or similar value is not a security
 * vulnerability. In such instances the vulnerability is the failure to remove
 * the examples web application.
 */
public class CookieFilter {

    private static final String OBFUSCATED = "[obfuscated]";

    private CookieFilter() {
        // Hide default constructor
    }

    public static String filter(String cookieHeader, String sessionId) {

        StringBuilder sb = new StringBuilder(cookieHeader.length());

        // Cookie name value pairs are ';' separated.
        // Session IDs don't use ; in the value so don't worry about quoted
        // values that contain ;
        StringTokenizer st = new StringTokenizer(cookieHeader, ";");

        boolean first = true;
        while (st.hasMoreTokens()) {
            if (first) {
                first = false;
            } else {
                sb.append(';');
            }
            sb.append(filterNameValuePair(st.nextToken(), sessionId));
        }


        return sb.toString();
    }

    private static String filterNameValuePair(String input, String sessionId) {
        int i = input.indexOf('=');
        if (i == -1) {
            return input;
        }
        String name = input.substring(0, i);
        String value = input.substring(i + 1, input.length());

        return name + "=" + filter(name, value, sessionId);
    }

    public static String filter(String cookieName, String cookieValue, String sessionId) {
        if (cookieName.toLowerCase(Locale.ENGLISH).contains("jsessionid") &&
                (sessionId == null || !cookieValue.contains(sessionId))) {
            cookieValue = OBFUSCATED;
        }

        return cookieValue;
    }
}

2.刪除test源碼目錄

3.刪除報錯的類

從新rebuild應該能夠成功

運行

配置"run configuration"

點擊:run-新建一個Application ,tomcat8.5.49

VM-OPTIONS

-Dcatalina.home=deploy
-Dcatalina.base=deploy
-Djava.endorsed.dirs=deploy/endorsed
-Djava.io.tmpdir=deploy/temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=deploy/conf/logging.properties
-Dfile.encoding=UTF8
-Duser.language=en
-Duser.region=US

其中後面三項是設置啓動後的亂碼問題

此時運行還會報錯,須要改一點代碼

/*添加JSP支持*/
        context.addServletContainerInitializer(new JasperInitializer(),null);

到這裏應該全部的問題都解決了,啓動,驗證

熟悉的8080端口提示

可能不熟悉的tom貓的管理界面

相關文章
相關標籤/搜索