首页 / TOMCAT / TomCat的工作原理和端口理解
TomCat的工作原理和端口理解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了TomCat的工作原理和端口理解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含18070字,纯文字阅读大概需要26分钟。
内容图文
![TomCat的工作原理和端口理解](/upload/InfoBanner/zyjiaocheng/1214/1fa45adfe6e147cf8b98744c4c57e4d2.jpg)
TomCat的工作原理和端口理解
关于解压官网下载:apache-tomcat-6.0.45.zip
解压后展现目录:
主要关注bin,conf,webapps这三个目录;bin目录里面会有启动的,停止的bat文件,conf主要是tomcat的配置文件,webapps主要是项目的发布文件,在webapps下,tomcat有自己的目录和文件
运行时,调用startup.bat文件:
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed withrem this work for additional information regarding copyright ownership.
rem The ASF licenses this fileto You under the Apache License, Version 2.0rem (the "License"); you may notuse this file except in compliance withrem the License. You may obtain a copy of the License at
remrem http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed toin writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions andrem limitations under the License.
if"%OS%" == "Windows_NT" setlocal
rem---------------------------------------------------------------------------rem Start script for the CATALINA Server
rem---------------------------------------------------------------------------rem Guess CATALINA_HOME ifnot defined
set "CURRENT_DIR=%cd%"ifnot"%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variableisnot defined correctly
echo This environment variableis needed to run this program
goto end
:okHome
set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This fileis needed to run this program
goto end
:okExec
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
:end
① 首先会根据web.xml文件提供的顺序穿件一个Catalina的servlet容器调用自己带的监听器,会监听程序的运行:
② 寻找CATALINA_HOME这个tomcat的环境变量,因为现在版本的tomcat放在一个叫Catalina的容器里,以前的tomcat的环境变量为TOMCAT_HOME,如果没有就将当前目录赋予为CATALINA_HOME这个目录,然后执行设置该目录为他运行的根目录
③ 在根目录下寻找catalina.bat文件并通过JAVA_HOME和JRE_HOME连接上JVM,该文件里面内容如下:
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed withrem this work for additional information regarding copyright ownership.
rem The ASF licenses this fileto You under the Apache License, Version 2.0rem (the "License"); you may notuse this file except in compliance withrem the License. You may obtain a copy of the License at
remrem http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed toin writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions andrem limitations under the License.
if"%OS%" == "Windows_NT" setlocal
rem---------------------------------------------------------------------------rem Start/Stop Script for the CATALINA Server
remrem Environment Variable Prerequisites
remrem CATALINA_HOME May point at your Catalina "build" directory.
remrem CATALINA_BASE (Optional) Base directory for resolving dynamic portions
remof a Catalina installation. Ifnot present, resolves torem the same directory that CATALINA_HOME points to.
remrem CATALINA_OPTS (Optional) Java runtime options used when the "start",
remor"run" command is executed.
remrem CATALINA_TMPDIR (Optional) Directory path location of temporary directory
rem the JVM should use (java.io.tmpdir). Defaults torem %CATALINA_BASE%\temp.
remrem JAVA_HOME Must point at your Java Development Kit installation.
rem Required to run the with the "debug" argument.
remrem JRE_HOME Must point at your Java Runtime installation.
rem Defaults to JAVA_HOME if empty.
remrem JAVA_OPTS (Optional) Java runtime options used when the "start",
rem"stop", or"run" command is executed.
remrem JAVA_ENDORSED_DIRS (Optional) Lists ofof semi-colon separated directories
rem containing some jars in order to allow replacement of APIs
rem created outside of the JCP (i.e. DOM and SAX from W3C).
rem It can also be used to update the XML parser implementation.
rem Defaults to $CATALINA_HOME/endorsed.
remrem JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start"rem command is executed. The defaultis"dt_socket".
remrem JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start"rem command is executed. The defaultis8000.remrem JPDA_SUSPEND (Optional) Java runtime options used when the "jpda start"rem command is executed. Specifies whether JVM should suspend
rem execution immediately after startup. Defaultis"n".
remrem JPDA_OPTS (Optional) Java runtime options used when the "jpda start"rem command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,
remand JPDA_SUSPEND are ignored. Thus, all required jpda
rem options MUST be specified. The defaultis:
remrem -agentlib:jdwp=transport=%JPDA_TRANSPORT%,
rem address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
remrem LOGGING_CONFIG (Optional) Override Tomcat‘s logging config filerem Example (all one line)
rem set LOGGING_CONFIG="-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties"remrem LOGGING_MANAGER (Optional) Override Tomcat‘s logging manager
rem Example (all one line)
rem set LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"remrem TITLE (Optional) Specify the title of Tomcat window. The defaultrem TITLE is Tomcat if it‘snot specified.
rem Example (all one line)
rem set TITLE=Tomcat.Cluster#1.Server#1 [%DATE% %TIME%]
rem---------------------------------------------------------------------------rem Guess CATALINA_HOME ifnot defined
set "CURRENT_DIR=%cd%"ifnot"%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variableisnot defined correctly
echo This environment variableis needed to run this program
goto end
:okHome
rem Copy CATALINA_BASE from CATALINA_HOME ifnot defined
ifnot"%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"
:gotBase
rem Ensure that any user defined CLASSPATH variables are not used on startup,
rem but allow them to be specified in setenv.bat, in rare casewhen it is needed.
set CLASSPATH=
rem Get standard environment variables
ifnot exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
call "%CATALINA_BASE%\bin\setenv.bat"
goto setenvDone
:checkSetenvHome
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
:setenvDone
rem Get standard Java environment variables
if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
echo This fileis needed to run this program
goto end
:okSetclasspath
set "BASEDIR=%CATALINA_HOME%"
call "%CATALINA_HOME%\bin\setclasspath.bat" %1if errorlevel 1 goto endifnot"%CATALINA_TMPDIR%" == "" goto gotTmpdir
set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
:gotTmpdir
rem Add tomcat-juli.jar and bootstrap.jar to classpath
rem tomcat-juli.jar can be over-ridden per instance
rem Note that there are no quotes as we do not want to introduce random
rem quotes into the CLASSPATH
if"%CLASSPATH%" == "" goto emptyClasspath
set "CLASSPATH=%CLASSPATH%;"
:emptyClasspath
if"%CATALINA_BASE%" == "%CATALINA_HOME%" goto juliClasspathHome
ifnot exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome
set "CLASSPATH=%CLASSPATH%%CATALINA_BASE%\bin\tomcat-juli.jar;%CATALINA_HOME%\bin\bootstrap.jar"
goto juliClasspathDone
:juliClasspathHome
set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"
:juliClasspathDone
ifnot"%LOGGING_CONFIG%" == "" goto noJuliConfig
set LOGGING_CONFIG=-Dnop
ifnot exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig
set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"
:noJuliConfig
set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%
ifnot"%LOGGING_MANAGER%" == "" goto noJuliManager
set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
:noJuliManager
set JAVA_OPTS=%JAVA_OPTS% %LOGGING_MANAGER%
rem----- Execute The Requested Command ---------------------------------------
echo Using CATALINA_BASE: "%CATALINA_BASE%"
echo Using CATALINA_HOME: "%CATALINA_HOME%"
echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"if""%1"" == ""debug"" goto use_jdk
echo Using JRE_HOME: "%JRE_HOME%"
goto java_dir_displayed
:use_jdk
echo Using JAVA_HOME: "%JAVA_HOME%"
:java_dir_displayed
echo Using CLASSPATH: "%CLASSPATH%"
set _EXECJAVA=%_RUNJAVA%
set MAINCLASS=org.apache.catalina.startup.Bootstrap
set ACTION=start
set SECURITY_POLICY_FILE=
set DEBUG_OPTS=
set JPDA=
ifnot""%1"" == ""jpda"" goto noJpda
set JPDA=jpda
ifnot"%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
set JPDA_TRANSPORT=dt_socket
:gotJpdaTransport
ifnot"%JPDA_ADDRESS%" == "" goto gotJpdaAddress
set JPDA_ADDRESS=8000
:gotJpdaAddress
ifnot"%JPDA_SUSPEND%" == "" goto gotJpdaSuspend
set JPDA_SUSPEND=n
:gotJpdaSuspend
ifnot"%JPDA_OPTS%" == "" goto gotJpdaOpts
set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
:gotJpdaOpts
shift
:noJpda
if""%1"" == ""debug"" goto doDebug
if""%1"" == ""run"" goto doRun
if""%1"" == ""start"" goto doStart
if""%1"" == ""stop"" goto doStop
if""%1"" == ""version"" goto doVersion
echo Usage: catalina ( commands ... )
echo commands:
echo debug Start Catalina in a debugger
echo debug -security Debug Catalina with a security manager
echo jpda start Start Catalina under JPDA debugger
echo run Start Catalina in the current window
echo run -security Start in the current window with security manager
echo start Start Catalina in a separate window
echo start -security Start in a separate window with security manager
echo stop Stop Catalina
echo version What version of tomcat are you running?
goto end
:doDebug
shift
set _EXECJAVA=%_RUNJDB%
set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"ifnot""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd
:doRun
shift
ifnot""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd
:doStart
shift
ifnot"%OS%" == "Windows_NT" goto noTitle
if"%TITLE%" == "" set TITLE=Tomcat
set _EXECJAVA=start "%TITLE%" %_RUNJAVA%
goto gotTitle
:noTitle
set _EXECJAVA=start %_RUNJAVA%
:gotTitle
ifnot""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd
:doStop
shift
set ACTION=stop
set CATALINA_OPTS=
goto execCmd
:doVersion
%_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo
goto end
:execCmd
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs
rem Execute Java with the applicable properties
ifnot"%JPDA%" == "" goto doJpda
ifnot"%SECURITY_POLICY_FILE%" == "" goto doSecurity
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doSecurity
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doJpda
ifnot"%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
%_EXECJAVA% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doSecurityJpda
%_EXECJAVA% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:end
④ 在加载java文件的时候,需要访问setclasspath.bat文件;来寻早到java的路径,如果没有配置JAVA_HOME和JAR_HOME环境变量,可以在路径寻找之前用set JAVA_HOME=”jdk所在路径”来设置访问路径。文件如下:
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed withrem this work for additional information regarding copyright ownership.
rem The ASF licenses this fileto You under the Apache License, Version 2.0rem (the "License"); you may notuse this file except in compliance withrem the License. You may obtain a copy of the License at
remrem http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed toin writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions andrem limitations under the License.
rem---------------------------------------------------------------------------rem Set CLASSPATH and Java options
rem---------------------------------------------------------------------------rem Make sure prerequisite environment variables are set
ifnot"%JAVA_HOME%" == "" goto gotJdkHome
ifnot"%JRE_HOME%" == "" goto gotJreHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variableis defined
echo At least one of these environment variableis needed to run this program
goto exit
:gotJreHome
ifnot exist "%JRE_HOME%\bin\java.exe" goto noJavaHome
ifnot exist "%JRE_HOME%\bin\javaw.exe" goto noJavaHome
ifnot""%1"" == ""debug"" goto okJavaHome
echo JAVA_HOME should point to a JDK in order to run in debug mode.
goto exit
:gotJdkHome
ifnot exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
ifnot exist "%JAVA_HOME%\bin\javaw.exe" goto noJavaHome
ifnot exist "%JAVA_HOME%\bin\jdb.exe" goto noJavaHome
ifnot exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
ifnot"%JRE_HOME%" == "" goto okJavaHome
set "JRE_HOME=%JAVA_HOME%"
goto okJavaHome
:noJavaHome
echo The JAVA_HOME environment variableisnot defined correctly
echo This environment variableis needed to run this program
echo NB: JAVA_HOME should point to a JDK not a JRE
goto exit
:okJavaHome
ifnot"%BASEDIR%" == "" goto gotBasedir
echo The BASEDIR environment variableisnot defined
echo This environment variableis needed to run this program
goto exit
:gotBasedir
if exist "%BASEDIR%\bin\setclasspath.bat" goto okBasedir
echo The BASEDIR environment variableisnot defined correctly
echo This environment variableis needed to run this program
goto exit
:okBasedir
rem Don‘t override the endorsed dir if the user has set it previously
ifnot"%JAVA_ENDORSED_DIRS%" == "" goto gotEndorseddir
rem Set the default -Djava.endorsed.dirs argument
set "JAVA_ENDORSED_DIRS=%BASEDIR%\endorsed"
:gotEndorseddir
rem Don‘t override _RUNJAVA if the user has set it previously
ifnot"%_RUNJAVA%" == "" goto gotRunJava
rem Set standard command for invoking Java.
rem Also note the quoting as JRE_HOME may contain spaces.
set _RUNJAVA="%JRE_HOME%\bin\java.exe"
:gotRunJava
rem Don‘t override _RUNJDB if the user has set it previously
rem Also note the quoting as JAVA_HOME may contain spaces.
ifnot"%_RUNJDB%" == "" goto gotRunJdb
set _RUNJDB="%JAVA_HOME%\bin\jdb.exe"
:gotRunJdb
goto end
:exitexit /b 1
:endexit /b 0
⑤ 在里这CATALINA_HOME文件夹下面的.bat文件加载完毕后,会加载conf下面catalina.properties文件,根据该配置文件的顺序,执行里面的server.xml调用项目的配置文件和tomcat-users.xml检查调用权限。
关于使用tomcat发布项目的四种方式:
- 直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法。
- 在tomcat安装目录中有一个conf文件夹,打开此文件夹,其中包含配置文件server.xml,打开配置文件,并在和之间插入如下语句。
其中,docBase为设置项目的路径。 - 在tomcat安装目录下的conf目录下面,打开server.xml文件,在标签之间,添加
<Servicename="culture">
<Connectorport="6011"maxHttpHeaderSize="8906"maxThreads="150"minSpareThreads="25"maxSpareThreads="75"enableLookups="false"redirectPort="8443"acceptCount="100"connectionTimeout="20000"disableUploadTimeout="true"URIEncoding="utf-8"useBodyEncodingForURI="true" />
<Enginename="culture"defaultHost="localhost">
<RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase" />
<Hostname="localhost"appBase="">
<Contextpath=""docBase="E:\ZRPDWorkspaces\MyEclipse Professional 2014\finance_culture\WebRoot"debug="5"reloadable="true"crossContext="true">
<Resourcename="jdbc/itfin"auth="Container"type="javax.sql.DataSource"maxActive="100"maxIdle="30"maxWait="10000"username="root"password="bigdata@mysql"driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://192.168.1.197:3306/culture?autoReconnect=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull"/>
</Context>
</Host>
</Engine>
</Service>
4.在conf目录中,在Catalina\localhost(此处需要注意目录的大小写)目录下新建一个XML文件,任意命名,只要和当前文件中的文件名不重复即可,代码如下。
第四种方法相对来说比较灵活,并且可以设置别名。该目录下面自身有两个tomcat容器的xml文件;
原文:http://blog.csdn.net/rentian1/article/details/51208703
内容总结
以上是互联网集市为您收集整理的TomCat的工作原理和端口理解全部内容,希望文章能够帮你解决TomCat的工作原理和端口理解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。