TomCat的工作原理和端口理解

来源:互联网 发布:淘宝小二介入处理时间 编辑:程序博客网 时间:2024/06/05 15:09

TomCat的工作原理和端口理解
关于解压官网下载:apache-tomcat-6.0.45.zip
解压后展现目录:

主要关注bin,conf,webapps这三个目录;bin目录里面会有启动的,停止的bat文件,conf主要是tomcat的配置文件,webapps主要是项目的发布文件,在webapps下,tomcat有自己的目录和文件

运行时,调用startup.bat文件:

@echo offrem Licensed to the Apache Software Foundation (ASF) under one or morerem contributor license agreements.  See the NOTICE file distributed withrem this work for additional information regarding copyright ownership.rem The ASF licenses this file to You under the Apache License, Version 2.0rem (the "License"); you may not use this file except in compliance withrem the License.  You may obtain a copy of the License atremrem     http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed to in writing, softwarerem 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" setlocalrem ---------------------------------------------------------------------------rem Start script for the CATALINA Serverrem ---------------------------------------------------------------------------rem Guess CATALINA_HOME if not definedset "CURRENT_DIR=%cd%"if not "%CATALINA_HOME%" == "" goto gotHomeset "CATALINA_HOME=%CURRENT_DIR%"if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomecd ..set "CATALINA_HOME=%cd%"cd "%CURRENT_DIR%":gotHomeif exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomeecho The CATALINA_HOME environment variable is not defined correctlyecho This environment variable is needed to run this programgoto end:okHomeset "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"rem Check that target executable existsif exist "%EXECUTABLE%" goto okExececho Cannot find "%EXECUTABLE%"echo This file is needed to run this programgoto end:okExecrem Get remaining unshifted command line arguments and save them in theset CMD_LINE_ARGS=:setArgsif ""%1""=="""" goto doneSetArgsset CMD_LINE_ARGS=%CMD_LINE_ARGS% %1shiftgoto setArgs:doneSetArgscall "%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 offrem Licensed to the Apache Software Foundation (ASF) under one or morerem contributor license agreements.  See the NOTICE file distributed withrem this work for additional information regarding copyright ownership.rem The ASF licenses this file to You under the Apache License, Version 2.0rem (the "License"); you may not use this file except in compliance withrem the License.  You may obtain a copy of the License atremrem     http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed to in writing, softwarerem 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" setlocalrem ---------------------------------------------------------------------------rem Start/Stop Script for the CATALINA Serverremrem Environment Variable Prerequisitesremrem   CATALINA_HOME   May point at your Catalina "build" directory.remrem   CATALINA_BASE   (Optional) Base directory for resolving dynamic portionsrem                   of a Catalina installation.  If not present, resolves torem                   the same directory that CATALINA_HOME points to.remrem   CATALINA_OPTS   (Optional) Java runtime options used when the "start",rem                   or "run" command is executed.remrem   CATALINA_TMPDIR (Optional) Directory path location of temporary directoryrem                   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 of of semi-colon separated directoriesrem                   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 default is "dt_socket".remrem   JPDA_ADDRESS    (Optional) Java runtime options used when the "jpda start"rem                   command is executed. The default is 8000.remrem   JPDA_SUSPEND    (Optional) Java runtime options used when the "jpda start"rem                   command is executed. Specifies whether JVM should suspendrem                   execution immediately after startup. Default is "n".remrem   JPDA_OPTS       (Optional) Java runtime options used when the "jpda start"rem                   command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,rem                   and JPDA_SUSPEND are ignored. Thus, all required jpdarem                   options MUST be specified. The default is: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's not specified.rem                   Example (all one line)rem                   set TITLE=Tomcat.Cluster#1.Server#1 [%DATE% %TIME%]rem ---------------------------------------------------------------------------rem Guess CATALINA_HOME if not definedset "CURRENT_DIR=%cd%"if not "%CATALINA_HOME%" == "" goto gotHomeset "CATALINA_HOME=%CURRENT_DIR%"if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomecd ..set "CATALINA_HOME=%cd%"cd "%CURRENT_DIR%":gotHomeif exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomeecho The CATALINA_HOME environment variable is not defined correctlyecho This environment variable is needed to run this programgoto end:okHomerem Copy CATALINA_BASE from CATALINA_HOME if not definedif not "%CATALINA_BASE%" == "" goto gotBaseset "CATALINA_BASE=%CATALINA_HOME%":gotBaserem Ensure that any user defined CLASSPATH variables are not used on startup,rem but allow them to be specified in setenv.bat, in rare case when it is needed.set CLASSPATH=rem Get standard environment variablesif not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHomecall "%CATALINA_BASE%\bin\setenv.bat"goto setenvDone:checkSetenvHomeif exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat":setenvDonerem Get standard Java environment variablesif exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspathecho Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"echo This file is needed to run this programgoto end:okSetclasspathset "BASEDIR=%CATALINA_HOME%"call "%CATALINA_HOME%\bin\setclasspath.bat" %1if errorlevel 1 goto endif not "%CATALINA_TMPDIR%" == "" goto gotTmpdirset "CATALINA_TMPDIR=%CATALINA_BASE%\temp":gotTmpdirrem Add tomcat-juli.jar and bootstrap.jar to classpathrem tomcat-juli.jar can be over-ridden per instancerem Note that there are no quotes as we do not want to introduce randomrem quotes into the CLASSPATHif "%CLASSPATH%" == "" goto emptyClasspathset "CLASSPATH=%CLASSPATH%;":emptyClasspathif "%CATALINA_BASE%" == "%CATALINA_HOME%" goto juliClasspathHomeif not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHomeset "CLASSPATH=%CLASSPATH%%CATALINA_BASE%\bin\tomcat-juli.jar;%CATALINA_HOME%\bin\bootstrap.jar"goto juliClasspathDone:juliClasspathHomeset "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar":juliClasspathDoneif not "%LOGGING_CONFIG%" == "" goto noJuliConfigset LOGGING_CONFIG=-Dnopif not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfigset LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties":noJuliConfigset JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%if not "%LOGGING_MANAGER%" == "" goto noJuliManagerset LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager:noJuliManagerset 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_jdkecho Using JRE_HOME:        "%JRE_HOME%"goto java_dir_displayed:use_jdkecho Using JAVA_HOME:       "%JAVA_HOME%":java_dir_displayedecho Using CLASSPATH:       "%CLASSPATH%"set _EXECJAVA=%_RUNJAVA%set MAINCLASS=org.apache.catalina.startup.Bootstrapset ACTION=startset SECURITY_POLICY_FILE=set DEBUG_OPTS=set JPDA=if not ""%1"" == ""jpda"" goto noJpdaset JPDA=jpdaif not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransportset JPDA_TRANSPORT=dt_socket:gotJpdaTransportif not "%JPDA_ADDRESS%" == "" goto gotJpdaAddressset JPDA_ADDRESS=8000:gotJpdaAddressif not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspendset JPDA_SUSPEND=n:gotJpdaSuspendif not "%JPDA_OPTS%" == "" goto gotJpdaOptsset JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%:gotJpdaOptsshift:noJpdaif ""%1"" == ""debug"" goto doDebugif ""%1"" == ""run"" goto doRunif ""%1"" == ""start"" goto doStartif ""%1"" == ""stop"" goto doStopif ""%1"" == ""version"" goto doVersionecho Usage:  catalina ( commands ... )echo commands:echo   debug             Start Catalina in a debuggerecho   debug -security   Debug Catalina with a security managerecho   jpda start        Start Catalina under JPDA debuggerecho   run               Start Catalina in the current windowecho   run -security     Start in the current window with security managerecho   start             Start Catalina in a separate windowecho   start -security   Start in a separate window with security managerecho   stop              Stop Catalinaecho   version           What version of tomcat are you running?goto end:doDebugshiftset _EXECJAVA=%_RUNJDB%set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"if not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd:doRunshiftif not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd:doStartshiftif not "%OS%" == "Windows_NT" goto noTitleif "%TITLE%" == "" set TITLE=Tomcatset _EXECJAVA=start "%TITLE%" %_RUNJAVA%goto gotTitle:noTitleset _EXECJAVA=start %_RUNJAVA%:gotTitleif not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd:doStopshiftset ACTION=stopset CATALINA_OPTS=goto execCmd:doVersion%_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfogoto end:execCmdrem Get remaining unshifted command line arguments and save them in theset CMD_LINE_ARGS=:setArgsif ""%1""=="""" goto doneSetArgsset CMD_LINE_ARGS=%CMD_LINE_ARGS% %1shiftgoto setArgs:doneSetArgsrem Execute Java with the applicable propertiesif not "%JPDA%" == "" goto doJpdaif not "%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:doJpdaif not "%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 offrem Licensed to the Apache Software Foundation (ASF) under one or morerem contributor license agreements.  See the NOTICE file distributed withrem this work for additional information regarding copyright ownership.rem The ASF licenses this file to You under the Apache License, Version 2.0rem (the "License"); you may not use this file except in compliance withrem the License.  You may obtain a copy of the License atremrem     http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed to in writing, softwarerem 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 optionsrem ---------------------------------------------------------------------------rem Make sure prerequisite environment variables are setif not "%JAVA_HOME%" == "" goto gotJdkHomeif not "%JRE_HOME%" == "" goto gotJreHomeecho Neither the JAVA_HOME nor the JRE_HOME environment variable is definedecho At least one of these environment variable is needed to run this programgoto exit:gotJreHomeif not exist "%JRE_HOME%\bin\java.exe" goto noJavaHomeif not exist "%JRE_HOME%\bin\javaw.exe" goto noJavaHomeif not ""%1"" == ""debug"" goto okJavaHomeecho JAVA_HOME should point to a JDK in order to run in debug mode.goto exit:gotJdkHomeif not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHomeif not exist "%JAVA_HOME%\bin\javaw.exe" goto noJavaHomeif not exist "%JAVA_HOME%\bin\jdb.exe" goto noJavaHomeif not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHomeif not "%JRE_HOME%" == "" goto okJavaHomeset "JRE_HOME=%JAVA_HOME%"goto okJavaHome:noJavaHomeecho The JAVA_HOME environment variable is not defined correctlyecho This environment variable is needed to run this programecho NB: JAVA_HOME should point to a JDK not a JREgoto exit:okJavaHomeif not "%BASEDIR%" == "" goto gotBasedirecho The BASEDIR environment variable is not definedecho This environment variable is needed to run this programgoto exit:gotBasedirif exist "%BASEDIR%\bin\setclasspath.bat" goto okBasedirecho The BASEDIR environment variable is not defined correctlyecho This environment variable is needed to run this programgoto exit:okBasedirrem Don't override the endorsed dir if the user has set it previouslyif not "%JAVA_ENDORSED_DIRS%" == "" goto gotEndorseddirrem Set the default -Djava.endorsed.dirs argumentset "JAVA_ENDORSED_DIRS=%BASEDIR%\endorsed":gotEndorseddirrem Don't override _RUNJAVA if the user has set it previouslyif not "%_RUNJAVA%" == "" goto gotRunJavarem Set standard command for invoking Java.rem Also note the quoting as JRE_HOME may contain spaces.set _RUNJAVA="%JRE_HOME%\bin\java.exe":gotRunJavarem Don't override _RUNJDB if the user has set it previouslyrem Also note the quoting as JAVA_HOME may contain spaces.if not "%_RUNJDB%" == "" goto gotRunJdbset _RUNJDB="%JAVA_HOME%\bin\jdb.exe":gotRunJdbgoto end:exitexit /b 1:endexit /b 0

⑤ 在里这CATALINA_HOME文件夹下面的.bat文件加载完毕后,会加载conf下面catalina.properties文件,根据该配置文件的顺序,执行里面的server.xml调用项目的配置文件和tomcat-users.xml检查调用权限。
关于使用tomcat发布项目的四种方式:

  1. 直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法。
  2. 在tomcat安装目录中有一个conf文件夹,打开此文件夹,其中包含配置文件server.xml,打开配置文件,并在和之间插入如下语句。

    其中,docBase为设置项目的路径。
  3. 在tomcat安装目录下的conf目录下面,打开server.xml文件,在标签之间,添加
<Service name="culture">      <Connector port="6011" maxHttpHeaderSize="8906"               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"               enableLookups="false" redirectPort="8443" acceptCount="100"               connectionTimeout="20000" disableUploadTimeout="true"           URIEncoding="utf-8" useBodyEncodingForURI="true" />    <Engine name="culture" defaultHost="localhost">      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"             resourceName="UserDatabase" />     <Host name="localhost" appBase="">       <Context path="" docBase="E:\ZRPDWorkspaces\MyEclipse Professional 2014\finance_culture\WebRoot"                 debug="5" reloadable="true" crossContext="true">             <Resource name="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&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull"/>           </Context>      </Host>    </Engine>      </Service>

4.在conf目录中,在Catalina\localhost(此处需要注意目录的大小写)目录下新建一个XML文件,任意命名,只要和当前文件中的文件名不重复即可,代码如下。

第四种方法相对来说比较灵活,并且可以设置别名。该目录下面自身有两个tomcat容器的xml文件;

0 0
原创粉丝点击