Idea中构建Tomcat8源码开发环境

编程

在研究SpringMVC源码之前得先看看Tomcat的源码(因为WEB应用要工作在容器中)。先构建Tomcat8的源码环境

下载

进入Tomcat官网https://tomcat.apache.org/download-80.cgi,选择tomcat8,下载下来是tomcat-8.5.49

导入到Idea中

准备

新建一个tomcat-8.5.49目录,再到其下新建一个deploy(是Tomcat的工作目录:-Dcatalina.home=x:\*.*deploy -Dcatalina.base=x:\*.*deploy)目录和导入源码目录,最终效果如下

deploy目录下的结构,除了可以复制的,其他目录直接新建

在tomcat-8.5.49目录下新建一个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>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,文件内容如下:

<?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项目

导入成功之后如下所示:

中间会遇到的问题:

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猫的管理界面

 

以上是 Idea中构建Tomcat8源码开发环境 的全部内容, 来源链接: utcz.com/z/511229.html

回到顶部