Tomcat详解 1、tomcat安装 tomcat的组件: <Server> <Service> <connector/> <connector/> <Engine> <Host /> <Host> <Context/> ... </Host> </Engine> </Service> </Server> 每个组件,均有java类来实现; 此些组件可分为如下几类: 顶级组件:Server 服务类:Service 连接器:http, https, ajp(apache jserv protocol) 容器类:Engine, Host, Context 被嵌套的组件:valve, logger, realm, loader, manager 集群类组件:listener, ...
OS版本CentOS 7
(1)系统自带的openjdk+tomcat
- 查看yum仓库中jdk版本: yum list all *jdk* 仓库中现有的openjdk版本为 java-1.6.0-openjdk java-1.7.0-openjdk java-1.8.0-openjdk - 本次测试使用java-1.7.0-openjdk: yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel -y - 设定java的环境变量($JAVA_HOME): 查看java目录 [root@localhost ~]# which java /usr/bin/java [root@localhost ~]# ls -l /usr/bin/java lrwxrwxrwx 1 root root 22 4月 26 09:46 /usr/bin/java -> /etc/alternatives/java [root@localhost ~]# ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 75 4月 26 09:46 /etc/alternatives/java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.13 1-2.6.9.0.el7_3.x86_64/jre/bin/java ~]# java -version 查看bin目录环境变量是否正常 java version "1.7.0_131" OpenJDK Runtime Environment (rhel-2.6.9.0.el7_3-x86_64 u131-b00) OpenJDK 64-Bit Server VM (build 24.131-b00, mixed mode) 添加$JAVA_HOME ~]# vim /etc/profile.d/java.sh export JAVA_HOME=/usr/bin ~]# . /etc/profile.d/java.sh ~]# echo $JAVA_HOME /usr/bin -tomcat安装 ~]# yum install tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat的配置文件查看 ~]# rpm -ql tomcat /etc/logrotate.d/tomcat /etc/sysconfig/tomcat /etc/tomcat /etc/tomcat/Catalina /etc/tomcat/Catalina/localhost /etc/tomcat/catalina.policy /etc/tomcat/catalina.properties /etc/tomcat/conf.d /etc/tomcat/conf.d/README /etc/tomcat/context.xml /etc/tomcat/log4j.properties /etc/tomcat/logging.properties /etc/tomcat/server.xml /etc/tomcat/tomcat-users.xml /etc/tomcat/tomcat.conf /etc/tomcat/web.xml /usr/bin/tomcat-digest /usr/bin/tomcat-tool-wrapper /usr/lib/systemd/system/tomcat.service /usr/lib/systemd/system/tomcat@.service /usr/libexec/tomcat /usr/libexec/tomcat/functions /usr/libexec/tomcat/preamble /usr/libexec/tomcat/server /usr/sbin/tomcat /usr/share/doc/tomcat-7.0.69 /usr/share/doc/tomcat-7.0.69/LICENSE /usr/share/doc/tomcat-7.0.69/NOTICE /usr/share/doc/tomcat-7.0.69/RELEASE-NOTES /usr/share/tomcat /usr/share/tomcat/bin/bootstrap.jar /usr/share/tomcat/bin/catalina-tasks.xml /usr/share/tomcat/conf /usr/share/tomcat/lib /usr/share/tomcat/logs /usr/share/tomcat/temp /usr/share/tomcat/webapps /usr/share/tomcat/work /var/cache/tomcat /var/cache/tomcat/temp /var/cache/tomcat/work /var/lib/tomcat /var/lib/tomcat/webapps /var/lib/tomcats /var/log/tomcat /var/log/tomcat/catalina.out - tomcat开启:8080和8009端口均正常开启 默认连接器: http, 8080 ajp, 8009 ~]# systemctl start tomcat.service ~]# ss -tnlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=1078,fd=3))LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1423,fd=13))LISTEN 0 100 :::8009 :::* users:(("java",pid=2942,fd=47))LISTEN 0 100 :::8080 :::* users:(("java",pid=2942,fd=46))LISTEN 0 128 :::22 :::* users:(("sshd",pid=1078,fd=4))LISTEN 0 100 ::1:25 :::* users:(("master",pid=1423,fd=14))(2)使用Oracle JDK和tomcat提供的编译完成的程序包
- Oracle JKD安装 ~]# rpm -ivh jdk-7u79-linux-x64.rpm 准备中... ################################# [100%] 正在升级/安装... 1:jdk-2000:1.7.0_79-fcs ################################# [100%] Unpacking JAR files... rt.jar... jsse.jar... charsets.jar... tools.jar... localedata.jar... jfxrt.jar... - java环境变量设置 查看java目录 ~]# ls -l /usr/java/ 总用量 4 lrwxrwxrwx 1 root root 16 4月 26 10:23 default -> /usr/java/latest drwxr-xr-x 8 root root 4096 4月 26 10:23 jdk1.7.0_79 lrwxrwxrwx 1 root root 21 4月 26 10:23 latest -> /usr/java/jdk1.7.0_79 java的bin目录 ~]# cd /usr/java/jdk1.7.0_79/ jdk1.7.0_79]# ls bin include LICENSE release THIRDPARTYLICENSEREADME.txt COPYRIGHT jre man src.zip db lib README.html THIRDPARTYLICENSEREADME-JAVAFX.txt jdk1.7.0_79]# ls bin appletviewer jarsigner javap jdb jps jvisualvm rmic tnameserv apt java java-rmi.cgi jhat jrunscript keytool rmid unpack200 ControlPanel javac javaws jinfo jsadebugd native2ascii rmiregistry wsgen extcheck javadoc jcmd jmap jstack orbd schemagen wsimport idlj javafxpackager jconsole jmc jstat pack200 serialver xjc jar javah jcontrol jmc.ini jstatd policytool servertool 设定环境变量,添加JAVA_HOME和PATH ~]# vim /etc/profile.d/java.sh JAVA_HOME=/usr/java/latest PATH=$JAVA_HOME/bin:$PATH export JAVA_HOME PATH ~]# . /etc/profile.d/java.sh ~]# echo $JAVA_HOME /usr/java/latest ~]# echo $PATH /usr/java/latest/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin ~]# java -version java version "1.7.0_79" Java(TM) SE Runtime Environment (build 1.7.0_79-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode) - 安装tomcat ~]# tar -xf apache-tomcat-7.0.55.tar.gz -C /usr/local/ ~]# cd /usr/local/ local]# ll 总用量 0 drwxr-xr-x 9 root root 149 4月 26 10:34 apache-tomcat-7.0.55 drwxr-xr-x. 2 root root 6 8月 12 2015 bin drwxr-xr-x. 2 root root 6 8月 12 2015 etc drwxr-xr-x. 2 root root 6 8月 12 2015 games drwxr-xr-x. 2 root root 6 8月 12 2015 include drwxr-xr-x. 2 root root 6 8月 12 2015 lib drwxr-xr-x. 2 root root 6 8月 12 2015 lib64 drwxr-xr-x. 2 root root 6 8月 12 2015 libexec drwxr-xr-x. 2 root root 6 8月 12 2015 sbin drwxr-xr-x. 5 root root 46 11月 7 16:32 share drwxr-xr-x. 2 root root 6 8月 12 2015 src local]# ln -s apache-tomcat-7.0.55 tomcat ocal]# cd tomcat/ tomcat]# ls 目录结构 bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work tomcat]# ls conf/ catalina.policy context.xml server.xml web.xml catalina.properties logging.properties tomcat-users.xml tomcat]# ls webapps/ docs examples host-manager manager ROOT tomcat]# ls bin/ bootstrap.jar commons-daemon-native.tar.gz digest.sh startup.bat tool-wrapper.sh catalina.bat configtest.bat setclasspath.bat startup.sh version.bat catalina.sh configtest.sh setclasspath.sh tomcat-juli.jar version.sh catalina-tasks.xml daemon.sh shutdown.bat tomcat-native.tar.gz commons-daemon.jar digest.bat shutdown.sh tool-wrapper.bat - 设定tomcat环境变量 ~]# vim /etc/profile.d/tomcat.sh CATALINA_BASE=/usr/local/tomcat PATH=$CATALINA_BASE/bin:$PATH export CATALINA_BASE PATH ~]# . /etc/profile.d/tomcat.sh ~]# catalina.sh --help Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Usage: catalina.sh ( commands ... ) commands: debug Start Catalina in a debugger debug -security Debug Catalina with a security manager jpda start Start Catalina under JPDA debugger run Start Catalina in the current window run -security Start in the current window with security manager start Start Catalina in a separate window start -security Start in a separate window with security manager stop Stop Catalina, waiting up to 5 seconds for the process to end stop n Stop Catalina, waiting up to n seconds for the process to end stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running configtest Run a basic syntax check on server.xml - check exit code for result version What version of tomcat are you running? Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined -开启tomcat ~]# catalina.sh configtest 检测配置文件是否正常 Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar 四月 26, 2017 10:40:10 上午 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environmen ts was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib四月 26, 2017 10:40:11 上午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-bio-8080"] 四月 26, 2017 10:40:11 上午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["ajp-bio-8009"] 四月 26, 2017 10:40:11 上午 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1328 ms ~]# catalina.sh start 开启tomcat Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. ~]# ss -tnlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=1071,fd=3))LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1429,fd=13))LISTEN 0 100 :::8009 :::* users:(("java",pid=16417,fd=43))LISTEN 0 100 :::8080 :::* users:(("java",pid=16417,fd=42))LISTEN 0 128 :::22 :::* users:(("sshd",pid=1071,fd=4))LISTEN 0 100 ::1:25 :::* users:(("master",pid=1429,fd=14)) 目录文件查看: ~]# cd /usr/local/tomcat/ tomcat]# ls bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work tomcat]# cd webapps/ [root@localhost webapps]# ls docs examples host-manager manager ROOT webapps]# tree manager/ manager/ ├── images │ ├── add.gif │ ├── asf-logo.gif │ ├── code.gif │ ├── design.gif │ ├── docs.gif │ ├── fix.gif │ ├── tomcat.gif │ ├── update.gif │ └── void.gif ├── index.jsp ├── META-INF │ └── context.xml ├── status.xsd ├── WEB-INF │ ├── jsp │ │ ├── 401.jsp │ │ ├── 403.jsp │ │ ├── 404.jsp │ │ ├── sessionDetail.jsp │ │ └── sessionsList.jsp │ └── web.xml └── xform.xsl 4 directories, 19 files webapps]# tree ROOT/ ROOT/ ├── asf-logo.png ├── asf-logo-wide.gif ├── bg-button.png ├── bg-middle.png ├── bg-nav-item.png ├── bg-nav.png ├── bg-upper.png ├── build.xml ├── favicon.ico ├── index.jsp ├── RELEASE-NOTES.txt ├── tomcat.css ├── tomcat.gif ├── tomcat.png ├── tomcat-power.gif ├── tomcat.svg └── WEB-INF └── web.xml 1 directory, 17 files webapps]# tree host-manager/ host-manager/ ├── images │ ├── add.gif │ ├── asf-logo.gif │ ├── code.gif │ ├── design.gif │ ├── docs.gif │ ├── fix.gif │ ├── tomcat.gif │ ├── update.gif │ └── void.gif ├── index.jsp ├── manager.xml ├── META-INF │ └── context.xml └── WEB-INF ├── jsp │ ├── 401.jsp │ ├── 403.jsp │ └── 404.jsp └── web.xmltomcat的配置文件:
server.xml:主配置文件; context.xml:每个webapp都可以有专用的配置文件,这些配置文件通常位于webapp程序目录下的WEB-INF目录中,用于定义会话管理顺、JDBC等 ;conf/context.xml是为各webapp提供默认配置; web.xml:每个webapp只有在“部署”之后才能够被访问;此文件则用于为各webapps定义默认的部署操作方式; tomcat-users.xml:用户认证的账号和密码配置文件; catalina.policy:当使用-security选项来启动tomcat实例时会读取此配置文件来实现基于安全策略的运行方式; catalina.properties:Java属性的定义文件,用于设定类加载器路径等 ,以及一些与JVM性能相关的调优参数; logging.properties:日志系统相关的配置;Java Webapp的组织结构:
有特定的组织形式、层次型的目录结构;主要包含了servlet代码文件、JSP页面文件、类文件、部署描述符文件等 ; /:webapps的根目录 index.jsp:jsp的主页面文件 WEB-INF/:当前webapp的私有资源目录,通常存放当前webapp自用的web.xml; META-INF/:当前webapp的私有资源目录,通常存储当前webapp自用的context.xml; classes/: 当前webapp的私有类; lib/:当前webapp的私有类,被打包成jar格式; webapp归档格式: .war:webapp .jar:EJB的类; .rar:资源适配器; .ear:企业级应用程序;