介绍

1.概念

Tomcat 服务器是一个开源的轻量级Web应用服务器,在中小型系统和并发量小的场合下被普遍使用,是开发和调试Servlet、JSP 程序的首选。

2.HTTP请求过程

Tomcat详细部署

Tomcat Server处理一个HTTP请求的过程

1、用户点击网页内容,请求被发送到本机端口8080,被在那里监听的Coyote HTTP/1.1 Connector获得。
2、Connector把该请求交给它所在的Service的Engine来处理,并等待Engine的回应。
3、Engine获得请求localhost/test/index.jsp,匹配所有的虚拟主机Host。
4、Engine匹配到名为localhost的Host(即使匹配不到也把请求交给该Host处理,因为该Host被定义为该Engine的默认主机),名为localhost的Host获得请求/test/index.jsp,匹配它所拥有的所有的Context。Host匹配到路径为/test的Context(如果匹配不到就把该请求交给路径名为“ ”的Context去处理)。
5、path=“/test”的Context获得请求/index.jsp,在它的mapping table中寻找出对应的Servlet。Context匹配到URL PATTERN为*.jsp的Servlet,对应于JspServlet类。
6、构造HttpServletRequest对象和HttpServletResponse对象,作为参数调用JspServlet的doGet()或doPost().执行业务逻辑、数据存储等程序。
7、Context把执行完之后的HttpServletResponse对象返回给Host。
8、Host把HttpServletResponse对象返回给Engine。
9、Engine把HttpServletResponse对象返回Connector。
10、Connector把HttpServletResponse对象返回给客户Browser。

Tomcat部署

1.java环境安装

安装jdk环境

[root@linfan ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel   

查看安装的版本

[root@linfan ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

tomcat部署

官网下载tomcat
下载tomcat

[root@linfan ~]cd /usr/src
[root@linfan src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz  
[root@linfan src]# ls
apache-tomcat-9.0.8.tar.gz  debug  kernels

解压部署

[root@linfan src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
[root@linfan src]# cd  /usr/local/
[root@linfan local]# ls
apache-tomcat-9.0.8  bin  etc  games  include  lib  lib64  libexec  nginx  sbin  share  src
[root@linfan local]# ln -s apache-tomcat-9.0.8/ tomcat
[root@linfan local]# ll
total 0
drwxr-xr-x   9 root root 160 Sep  4 23:00 apache-tomcat-9.0.8
drwxr-xr-x.  2 root root   6 Nov  5  2016 bin
drwxr-xr-x.  2 root root   6 Nov  5  2016 etc
drwxr-xr-x.  2 root root   6 Nov  5  2016 games
drwxr-xr-x.  2 root root   6 Nov  5  2016 include
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib64
drwxr-xr-x.  2 root root   6 Nov  5  2016 libexec
drwxr-xr-x. 11 root root 151 Sep  3 03:16 nginx
drwxr-xr-x.  2 root root   6 Nov  5  2016 sbin
drwxr-xr-x.  5 root root  49 Aug 30 04:37 share
drwxr-xr-x.  2 root root   6 Nov  5  2016 src
lrwxrwxrwx   1 root root  20 Sep  4 23:01 tomcat -> apache-tomcat-9.0.8/

写一个hello world的java页面

[root@linfan ~]# cd /usr/local/tomcat/webapps/
[root@linfan webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@linfan webapps]# mkdir test
[root@linfan webapps]# vim index.jsp
[root@linfan webapps]# ls
docs  examples  host-manager  index.jsp  manager  ROOT  test
[root@linfan webapps]# vim index.jsp
[root@linfan webapps]# rm -rf index.jsp
[root@linfan webapps]# rm -rf test/
[root@linfan webapps]# cd
[root@linfan ~]#  cd /usr/local/tomcat/webapps/
[root@linfan webapps]# mkdir test
[root@linfan webapps]# cd test
[root@linfan test]# vim index.jsp
[root@linfan test]# cat index.jsp
<html>
<head>
        <title>test page</title>
</head>
<body>
      <%
         out.println("Hellow World");
      %>
</body>
</html>

启动tomcat

[root@linfan test]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@linfan test]# ss -natl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port
LISTEN     0      128                               *:22                                            *:*
LISTEN     0      100                       127.0.0.1:25                                            *:*
LISTEN     0      100                              :::8080                                         :::*
LISTEN     0      128                              :::22                                           :::*
LISTEN     0      100                             ::1:25                                           :::*
LISTEN     0      1                  ::ffff:127.0.0.1:8005                                         :::*
LISTEN     0      100                              :::8009                                         :::*
[root@linfan test]# ps -ef |grep tomcat
root       2041      1 14 23:24 pts/1    00:00:03 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root       2092   1905  0 23:25 pts/1    00:00:00 grep --color=auto tomcat

在浏览器上输入192.168.24.146:8080/test浏览网页
Tomcat详细部署