Tomcat部署之URL映射

参考Tomcat官方文档Tomcat Document.

     通常我们在手动部署项目时,是将[projectName].war直接放到[tomcat目录]/webapps目录下,那么你的项目默认的URL是:http://localhost:8080/[projectName]. 有时候这个路径并不是你想要的,那么该如何修改默认的URL呢?

     在[tomcat目录]/webapps目录下,有一个ROOT目录,这个就是Tomcat默认的项目,其映射路径是http://localhost:8080.下面我们来简单介绍修改URL的三种方法。

     第一种:删除ROOT目录

     在你删除ROOT目录前,最好先做一个备份。首先我们需要停止运行tomcat,然后删除ROOT目录,将[projectName].war重命名为ROOT.war(必须大写)。然后将你的ROOT.war直接放到[tomcat目录]/webapps目录下。重启tomcat, 那么你的项目就会成为Tomcat默认的项目啦,其映射路径为tomcat根目录,即:http://localhost:8080。

    第二种:ContextName.xml(推荐使用)

    首先将你的war包从[tomcat目录]/webapps目录下移出来,放入电脑中除tomcat目录]/webapps的任意目录(这是为了避免重复部署)。在[tomcat目录]/conf/[engine name]/[host name]目录下新建一个[ContexName].xml。通常[tomcat目录]/conf目录下有默认的contex.xml。将其复制到前面所述的目录重命名,然后稍微修改即可。Tomcat默认的engine是Catalina,默认的host是localhost,可以通过server.xml中修改与查看。因此[ContexName].xml需要放入的路径是tomcat目录]/conf/Catalina/localhost/[ContexName].xml。

     值得注意的是[ContexName].xml中必须有 docBase属性,该属性指向你war包在你电脑中的存放路径。

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->




<Context docBase ="/home/***/apache-tomcat-9.0.6/emall.war"  debug ="0"  privileged ="true"  reloadable ="false">


   
</Context>

    不需要写path元素是因为该方式下默认path为ContextName. 例如你的xml为emall.xml,那么映射路径为http://localhost:8080/emall。如果你需要映射为tomcat根目录,即http://localhost:8080,那么你只需要将其命名为ROOT.xml,同时需要删除 [tomcat目录]/webapps目录下的ROOT目录 ,然后重启tomcat即可使用。  

      第三种:修改server.xml(不推荐)

        第一步:在修改server.xml前先备份。同样地,你需要将war包从[tomcat目录]/webapps目录下移出来。

        第二步:修改server.xml。将host的autoDeploy与deployOnStartup设置为false.

        第三步: 在<host></host>中间添加Context, 指定path以及docBase.

<Context  path ="/"  reloadable ="false"  docBase ="/home/***/apache-tomcat-9.0.6/emall.war"  />

图中映射路径是根/路径,如果想要映射为根路径还需要删除 [tomcat目录]/webapps目录下的ROOT目录 ,然后重启tomcat即可使用。

猜你喜欢

转载自blog.csdn.net/zlf1227/article/details/82689397
今日推荐