1.下载Tomcat源码:
tomcat8.5.57下载
(以上是Tomcat8.5.57下载地址)
2.解压,然后在根目录下新建一个文件夹(可自定义名字)home,并且将 conf 和 webapps 移动到该文件夹
3.因为Tomcat是Java写的,所以可以直接在根目录创建一个 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>apache-tomcat-8.5.57-src</artifactId> <name>Tomcat8.5</name> <version>8.5</version> <build> <!--指定源目录--> <finalName>Tomcat8.5</finalName> <sourceDirectory>java</sourceDirectory> <resources> <resource> <directory>java</directory> </resource> </resources> <plugins> <!--引入编译插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <encoding>UTF-8</encoding> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <!--tomcat 依赖的基础包--> <dependencies> <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>4.新建启动项
VM options内容如下:
-Dcatalina.home=D:/apache-tomcat-8.5.57-src/home -Dcatalina.base=D:/apache-tomcat-8.5.57-src/home -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=D:/apache-tomcat-8.5.57-src/home/conf/logging.properties根据自己新建的文件夹位置替换上面的路径:
5.添加JSP初始化解析引擎
找到org.apache.catalina.startup包下的 ContextConfig 类的 configureStart 方法,向方法中加入以下代码:
context.addServletContainerInitializer(new JasperInitializer(), null);如图:
6.解决乱码问题
step1. 找到org.apache.jasper.compiler包Localizer类getMessage()方法,加入以下代码:
// 处理乱码问题 try { errMsg = new String(errMsg.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }如图:
step2. 找到org.apache.tomcat.util.res包StringManager类getString()方法,加入以下代码:
// 处理乱码问题 try { str = new String(str.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }如图: