Tomcat简介 按我的理解Tomcat是一个容器,存储一些jsp,servlet类。同时负责客户端发送的http请求转发到servlet容器(jsp的本质就是servlet,servlet类可以理解成为controller层的Java类)。
Tomcat的安装 进入官网。如tomcat8.5.72,点击64位的window压缩包下载64-bit Windows zip。连接如下https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.72/bin/apache-tomcat-8.5.72-windows-x64.zip
,然后解压即可。也可以下载源码https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.72/src/apache-tomcat-8.5.72-src.zip
Tomcat服务的启动
1.可以直接点击startup.bat。【在你的tomcat目录\bin】
2.也可以把tomcat源码下载下来,导入maven工程,配置启动类运行。
下面我使用第二种方式启动tomcat容器,我的文件结构是如图1。首先在idea中创建一个空的项目tomcatTest11,再把源码的解压包放在空目录tomcatTest11,然后在apache-tomcat-8.5.42-src下新建一个pom.xml文件,点击File,在点击Project from existing source,选择pom.xml文件就可以导入工程了,然后添加启动类Application。具体流程如下图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 <?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.5</artifactId > <name > Tomcat8.5</name > <version > 8.5</version > <build > <finalName > Tomcat8.5</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 > </dependencies > </project >
此时点击debug运行boostrap启动类启动tomcat,可能会报错误,若出现了,CookieFilter类找不到,请参照此链接https://blog.csdn.net/sun54429552/article/details/69666315
;若tomcat没有报错就在页面上输入localhost:8080。若此时页面上出现org.apache.jasper.servlet.JasperInitializer
上图的原因是我们直接启动org.apache.catalina.startup.Bootstrap的时候没有加载org.apache.jasper.servlet.JasperInitializer,从而无法编译JSP。解决办法是在tomcat的源码org.apache.catalina.startup.ContextConfig中的configureStart函数中手动将JSP解析器初始化:
为什么启动类是Bootstrap.java 在tomcat的解压包下,有个bin目录,其中startup.bat脚本的是启动tomcat的,可以点开一摊究竟。
总结 可能出现问题的有两种情况,第一个是没有找到CookieFilter类,请在test包下的util包中添加CookieFilter,第二种情况是tomcat启动不报错但是访问localhost:8080出现空指针异常org.apache.jasper.servlet.JasperInitializer,解决方法是倒数第二个图。
参考视频 https://www.bilibili.com/video/BV1dJ411N7Um?p=5