Tomcat是一个开源的Java Servlet容器,可以作为Java Web应用程序的运行环境。它是Apache软件基金会的一个重要项目,具有高性能、稳定可靠以及跨平台等特点。它实现了Java Servlet和JavaServer Pages(JSP)规范,并提供了一个运行环境,使得Java代码可以在Web服务器上执行。
tomcat就是传说中的中间件之一,tomcat本身是一个容器,专门用来运行java程序,java语言开发的网页.jsp就应该运行于tomcat中。而tomcat本身的运行也依赖于jdk环境。
omcat的工作原理基于Java Servlet和JavaServer Pages(JSP)规范,它充当了一个Java Web应用程序的运行环境和Web服务器。以下是Tomcat的工作原理的详细说明:
总之,Tomcat的工作原理涉及连接器、容器、Servlet容器、JSP引擎以及部署描述符等多个组件,它们协同工作来处理HTTP请求,将请求传递给适当的Servlet或JSP,生成响应并将其返回给客户端。这个过程使得Tomcat成为一个强大的Java Web应用服务器,用于部署和运行Java Web应用程序。
tomcat的工作原理基于Java Servlet和JSP规范。它充当一个Web服务器,接收HTTP请求并将其传递给合适的Servlet或JSP进行处理。以下是Tomcat的基本工作流程:
服务器 | IP地址 | 系统版本 | tomcat版本 |
---|---|---|---|
tomcat | 192.168.195.137 | centos 8 | apache-tomcat-9.0.80.tar.gz |
首先关闭防火墙和selinux
[root@tomcat ~]# systemctl disable --now firewalld.service Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@tomcat ~]# setenforce 0 [root@tomcat ~]# vim /etc/selinux/config [root@tomcat ~]# grep '^SELINUX=' /etc/selinux/config SELINUX=disabled
JDK(Java Development Kit)和JRE(Java Runtime Environment)是Java开发和运行环境的两个重要组成部分,它们之间有以下区别:
JDK:
JRE:
简而言之,JDK适用于开发者,提供了编译、调试和开发所需的工具和库。而JRE适用于用户,用于运行Java应用程序,不包含开发工具。如果你只是要运行Java程序,只需要安装JRE即可。如果你要进行Java应用程序的开发,需要安装JDK。
//安装jdk环境 [root@tomcat ~]# rm -rf /etc/yum.repos.d/* [root@tomcat ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo [root@tomcat ~]# yum makecache [root@tomcat ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel wget vim 省略 . . . //查看安装的版本,能够查看到版本则说明安装成功 [root@tomcat ~]# java -version openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-b07) OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)
进入tomcat官网
Apache Tomcat® - Welcome!
//拉取tomcat软件包 [root@tomcat ~]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.80/bin/apache-tomcat-9.0.80.tar.gz --2023-10-10 13:41:29-- https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.80/bin/apache-tomcat-9.0.80.tar.gz Resolving archive.apache.org (archive.apache.org)... 65.108.204.189, 2a01:4f9:1a:a084::2 Connecting to archive.apache.org (archive.apache.org)|65.108.204.189|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 11692853 (11M) [application/x-gzip] Saving to: ‘apache-tomcat-9.0.80.tar.gz’ apache-tomcat-9.0.80.tar.gz 100%[===============================================>] 11.15M 25.2KB/s in 5m 55s 2023-10-10 13:47:25 (32.1 KB/s) - ‘apache-tomcat-9.0.80.tar.gz’ saved [11692853/11692853] [root@tomcat ~]# ls anaconda-ks.cfg apache-tomcat-9.0.80.tar.gz
//解压至指定目录 [root@tomcat ~]# tar xf apache-tomcat-9.0.80.tar.gz -C /usr/local/ [root@tomcat ~]# ls /usr/local/ apache-tomcat-9.0.80 bin etc games include lib lib64 libexec sbin share src //设置tomcat软链接,方便后续如果更换tomcat版本后也能直接使用 [root@tomcat ~]# cd //usr/local/ [root@tomcat local]# ll total 0 drwxr-xr-x 9 root root 220 Oct 10 13:49 apache-tomcat-9.0.80 drwxr-xr-x. 2 root root 6 Aug 12 2018 bin drwxr-xr-x. 2 root root 6 Aug 12 2018 etc drwxr-xr-x. 2 root root 6 Aug 12 2018 games drwxr-xr-x. 2 root root 6 Aug 12 2018 include drwxr-xr-x. 2 root root 6 Aug 12 2018 lib drwxr-xr-x. 2 root root 6 Aug 12 2018 lib64 drwxr-xr-x. 2 root root 6 Aug 12 2018 libexec drwxr-xr-x. 2 root root 6 Aug 12 2018 sbin drwxr-xr-x. 5 root root 49 Jul 20 11:24 share drwxr-xr-x. 2 root root 6 Aug 12 2018 src [root@tomcat local]# ln -s apache-tomcat-9.0.80 tomcat [root@tomcat local]# ll total 0 drwxr-xr-x 9 root root 220 Oct 10 13:49 apache-tomcat-9.0.80 drwxr-xr-x. 2 root root 6 Aug 12 2018 bin drwxr-xr-x. 2 root root 6 Aug 12 2018 etc drwxr-xr-x. 2 root root 6 Aug 12 2018 games drwxr-xr-x. 2 root root 6 Aug 12 2018 include drwxr-xr-x. 2 root root 6 Aug 12 2018 lib drwxr-xr-x. 2 root root 6 Aug 12 2018 lib64 drwxr-xr-x. 2 root root 6 Aug 12 2018 libexec drwxr-xr-x. 2 root root 6 Aug 12 2018 sbin drwxr-xr-x. 5 root root 49 Jul 20 11:24 share drwxr-xr-x. 2 root root 6 Aug 12 2018 src lrwxrwxrwx 1 root root 20 Oct 10 13:51 tomcat -> apache-tomcat-9.0.80 [root@tomcat local]# cd tomcat/ [root@tomcat tomcat]# pwd //usr/local/tomcat [root@tomcat tomcat]# ls bin conf lib logs README.md RUNNING.txt webapps BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work [root@tomcat tomcat]# ls bin/ bootstrap.jar ciphers.sh daemon.sh setclasspath.bat startup.sh version.bat catalina.bat commons-daemon.jar digest.bat setclasspath.sh tomcat-juli.jar version.sh catalina.sh commons-daemon-native.tar.gz digest.sh shutdown.bat tomcat-native.tar.gz catalina-tasks.xml configtest.bat makebase.bat shutdown.sh tool-wrapper.bat ciphers.bat configtest.sh makebase.sh startup.bat tool-wrapper.sh 上述的bin目录中我们能够用到的catalina.sh、shutdown.sh、version.sh、startup.sh、configtest.sh 这几个执行文件,用途分别是: catalina.sh 能够关闭和开启服务(常用) shutdown.sh 只能关闭服务(不是关闭主机) startup.sh 只能开启服务 version.sh 查看tomcat版本 configtest.sh 测试配置文件是否存在错误,检查配置文件 //将tomcat的lib位置存放在/etc/ld.so.conf/d/下面,命名一个自身名字的文件,方便查找 [root@tomcat tomcat]# ls bin conf lib logs README.md RUNNING.txt webapps BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work [root@tomcat tomcat]# vim /etc/ld.so.conf.d/tomcat.conf [root@tomcat tomcat]# cat /etc/ld.so.conf.d/tomcat.conf /usr/local/tomcat/lib [root@tomcat tomcat]# ldconfig -v ldconfig: Can't stat /libx32: No such file or directory ldconfig: Path `/usr/lib' given more than once ldconfig: Path `/usr/lib64' given more than once ldconfig: Can't stat /usr/libx32: No such file or directory /usr/lib64//bind9-export: libisccfg-export.so.163 -> libisccfg-export.so.163.0.4 libisc-export.so.1104 -> libisc-export.so.1104.0.0 libirs-export.so.161 -> libirs-export.so.161.0.0 libdns-export.so.1107 -> libdns-export.so.1107.1.0 /usr/local/tomcat/lib: /lib: /lib64:
//使用绝对路径执行/usr/local/tomcat/bin/下面的脚本,tomcat不能写进环境变量,放置后续更改tomcat版本后环境变量仍是之前的tomcat版本 [root@tomcat tomcat]# pwd //usr/local/tomcat [root@tomcat tomcat]# cd bin [root@tomcat bin]# pwd //usr/local/tomcat/bin [root@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 Using CATALINA_OPTS: Tomcat started. [root@tomcat bin]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:* LISTEN 0 100 *:8080 *:* LISTEN 0 128 [::]:22 [::]:* [root@tomcat test]# ps -ef | grep tomcat //查看tomcat进程 root 1585 1 0 14:02 pts/0 00:00:06 /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 1640 1455 0 14:22 pts/0 00:00:00 grep --color=auto tomcat 服务启动成功
在部署tomcat过程中,网页存放目录在/usr/local/tomcat/webapps中,里面可以存放我们的网页,也可以放置app软件,而我们上述所访问到的页面则来自webapp目录中的ROOT目录中的项目
[root@tomcat tomcat]# ls bin conf lib logs README.md RUNNING.txt webapps BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work [root@tomcat tomcat]# cd webapps/ [root@tomcat webapps]# ls docs examples host-manager manager ROOT
我们新建一个项目目录进行测试
[root@tomcat webapps]# mkdir test && cd test [root@tomcat test]# vim index.jsp [root@tomcat test]# cat index.jsptest page <% out.println("Hellow World"); %> [root@tomcat test]#
访问test项目
访问那个项目就在端口后加哪个项目名,不加则默认是ROOT
解决方法:
进入tomcat的conf目录中修改tomcat-user.xml文件 [root@tomcat tomcat]# cd conf/ [root@tomcat conf]# ls Catalina catalina.properties jaspic-providers.xml logging.properties tomcat-users.xml web.xml catalina.policy context.xml jaspic-providers.xsd server.xml tomcat-users.xsd [root@tomcat conf]# vim tomcat-users.xml [root@tomcat conf]# tail -3 tomcat-users.xml//设置角色 //设置用户,密码,并指定角色 进入tomcat的项目文件(webapps)中任选一个项目 [root@tomcat conf]# cd ../webapps/ [root@tomcat webapps]# ls docs examples host-manager manager ROOT test [root@tomcat webapps]# cd manager/ [root@tomcat manager]# ls css images index.jsp META-INF status.xsd WEB-INF xform.xsl [root@tomcat manager]# cd META-INF/ [root@tomcat META-INF]# ls context.xml [root@tomcat META-INF]# vim context.xml //在allow中加入本机的ip网段 [root@tomcat META-INF]# grep -A1 '