SoFunction
Updated on 2025-03-03

Tomcat how to remove project name and access project directly

Tomcat removes the project name and accesses the project directly

need:

Release the web project published to Tomcat, and remove the project name from the access path.

That is to say, the web page I posted on the server does not need to add the project name, and you can access it directly according to the port number.

Method 1

principle:

The default root directory of Tomcat is ROOT. In fact, the ROOT project is useless in the actual production environment, so we can use our project to cover the ROOT project.

Operation process:

1. Delete all files and folders under ROOT

2. After unzipping the war package of our project, copy all files and subdirectories in the project directory to the ROOT directory. Or simply: delete the ROOT directory directly, and then change the name of our project package to and put it under webapps.

Method 2: (Recommended)

principle:

Tomcat itself can configure virtual directories. The method is to add Context information under the middle node.

If we can configure <Context path="/abc" docBase="D:\app\abc" .../>, then we can access the abc project we placed under D:\app\ through the address http://localhost:8080/abc.

We can modify this path="/abc" to path="".

It means to map abc to the root directory, and the access path will become http://localhost:8080/.

Operation process:

According to the way of configuring the virtual directory, add a Context node below. The specific configuration is as follows:

&lt;Engine name="Catalina" defaultHost="localhost"...&gt;  
...  
    &lt;Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true"&gt;  
    &lt;Context path="" docBase="Your project name" reloadable="true" /&gt;  
&lt;!--Note:I'm using relative paths here,The project is placedTomcatofwebapps目录下of,Of course it can also be changed to an absolute path--&gt;  
...  
    &lt;/Host&gt;  
...  
&lt;/Engine&gt;

If the port is 8080, you can access it directly at http://ip:8080

After removing the project name, the solution to the access path still has problems

After Tomcat removes the project name, you can access the home page, but you can't access other pages.

After publishing your personal blog to the server, the package is placed in the /usr/local/tomcat/apache-tomcat-8.5.66/webapps directory, and then open the following configuration. In theory, the project name that the path access can be removed (the project name here is blog).

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
    <Context path="" docBase="/usr/local/tomcat/webapps/blog" reloadable="false"/>

Problems that arise

http://161.62.71.204/Only access the home page, access other pages and report 404

After visiting http://161.62.71.204/, you can indeed enter the blog homepage, but click on other blog pages to report 404, such as visiting http://161.62.71.204/blog/44, but the file cannot be found.

Later I found out that the project name must be added to access all other pages, such as http://161.62.71.204/blog/blog/44

This does not conform to the original intention of removing the project name.

Solution

Change it to "webapps/project name" in appBase="webapps"

<Host name="localhost"  appBase="webapps/blog"
            unpackWARs="true" autoDeploy="true">
    <Context path="" docBase="/usr/local/tomcat/webapps/blog" reloadable="false"/>

You can access all pages at http://161.62.71.204/

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.