通常情况下,我们配置好了配置文件后,就开始启动start.bat批处理文件,进行tomcat的启动。
那么tomcat是如何一步步启动的呢?我们接下来就来详细介绍一下,tomcat的启动过程。
通过阅读start.bat批处理文件,我们不难发现,它其实就是检查catalina.bat 执行所需环境,并调用catalina.bat 批处理文件。接下来我们详细解释一下start.bat文件的内容。
start.bat文件内容如下:
@echo off //此命令代表:只显示执行命令后的结果,不显示命令本身
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in 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 and
rem limitations under the License.
rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem ---------------------------------------------------------------------------
setlocal //设置本机的环境变量
rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%" //设置CURRENT_DIR变量为(当前工作目录)D:\01-计算机\win8\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36\bin
if not "%CATALINA_HOME%" == "" goto gotHome //判断CATALINA_HOME变量是否配置,如果配置了则跳转到 goHome命令
set "CATALINA_HOME=%CURRENT_DIR%" //如果没有设置则设置CATALINA_HOME为当前目录,变量值为:D:\01-计算机\win8\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36\bin
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome //如果当前工作目录存在catalina.bat文件,则跳转到okHome命令
cd .. //回到上级目录 D:\01-计算机\win8\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36
set "CATALINA_HOME=%cd%" //更改CATALINA_HOME变量值为:D:\01-计算机\win8\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36
cd "%CURRENT_DIR%" //切换当前工作目录
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome //如果当前工作目录存在catalina.bat文件,则跳转到okHome命令
echo The CATALINA_HOME environment variable is not defined correctly //提示相应的错误信息
echo This environment variable is needed to run this program
goto end
:okHome
set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" // 设置EXECUTABLE(执行文件)变量
rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec //如果存在EXECUTABLE变量,则执行okExec命令
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExec
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS= //清除CMD_LINE_ARGS环境变量原来的值
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 //给 CMD_LINE_ARGS 添加参数,并以空格隔开
shift
goto setArgs
:doneSetArgs
call "%EXECUTABLE%" start %CMD_LINE_ARGS% //打开新窗口,执行 EXECUTABLE 环境变量所代表的catalina.bat 批处理文件与 CMD_LINE_ARGS 代表的参数
:end //结束
通过观察发现,start.bat和shutdown.bat文件的区别就是执行参数不同,在start.bat中是:call "%EXECUTABLE%" start %CMD_LINE_ARGS%,而在shutdown.bat中是stop。而真正的启动和关闭tomcat的过程是在catalina.bat中执行的。只让我想到了我们的函数封装,以及多态的特点。相同的执行过程,只因为传递的参数不同,而得到不同的结果。