免配置环境变量使用Tomcat+设置项目主页路径为http://localhost:8080+修改tomcat端口号

时间:2022-12-12 10:09:40

一、免配置jdk JAVA_HOME和tomcat  CATALINA_HOME环境变量使用tomcat

  众说周知,使用tomcat需要有java环境,一般情况下需要配置jdk和tomcat的路径到windows系统的环境变量中。但是也可以不用配置环境变量,直接编辑tomcat的startup.bat文件即可,下面是TOMCAT安装路径bin目录下的startup.bat中的代码,红色字体就是配置了。其实这种方法适用于其它程序,例如:MAVEN的配置,maven运行也需要JAVA_HOME,可以在maven的bin目录下的mvn.cmd中做类似的配置即可。

  

@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 ---------当前bat文件目录的上级--------
set CATALINA_HOME=%~dp0..

rem -----------setclasspath.bat中要用到JAVA_HOME----------
set JAVA_HOME=E:\Program Files\Java\jdk1.8.0_131


rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%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 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"

rem Check that target executable exists
if exist "%EXECUTABLE%" goto 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=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end
pause

二、设置项目主页路径为http://localhost:8080、修改tomcat端口号

  tomcat文件夹下的conf文件夹下有个server.xml,port后面的数字就是端口号,修改就好了(有三个),如果只是修改访问端口(如:http://localhost:8080),只需要修改第二个就可以了。

  通过添加<Context path="" docBase="E:\Program Files\apache-tomcat-8.0.37\webapps\fire360-3.0" debug="0" reloadable = "true" />这一行,来修改tomcat的主页路径为项目的主页。

<?xml version='1.0' encoding='utf-8'?>

<Server port="8006" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />


  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
               
        <Context path="" docBase="E:\Program Files\apache-tomcat-8.0.37\webapps\fire360-3.0" debug="0" reloadable = "true" />
        
      </Host>
    </Engine>
  </Service>
</Server>