1. Overview
In this quick article, we’ll discuss deploying a web application at the root of a Tomcat.
2. Tomcat Deployment Basics and Terminology
First, the basics of deploying an application to Tomcat can be found in this guide: How to Deploy a WAR File to Tomcat.
Simply put, web applications are placed under $CATALINA_HOME\webapps, where $CATALINA_HOME is the Tomcat’s installation directory.
The context path refers to the location relative to the server’s address which represents the name of the web application.
By default, Tomcat derives it from the name of the deployed war-file. So if we deploy a file ExampleApp.war, it will be available at http://localhost:8080/ExampleApp. I. e. the context path is /ExampleApp.
If we now need to have that app available at http://localhost:8080/ instead, we have a few options, which we’ll discuss in the following sections.
For a more detailed explanation of the context concept of Tomcat, have a look at the official Tomcat documentation.
3. Deploying the App as ROOT.war
The first option is very straightforward: we just have to delete the default /ROOT/ folder in $CATALINA_HOME\webapps, rename our ExampleApp.war to ROOT.war, and deploy it.
Our app will now be available at http://localhost:8080/.
4. Specifying the Context Path in the server.xml
The second option is to set the context path of the application in the server.xml (which is located at $CATALINA_HOME\conf).
We must insert the following inside the
<Context path="" docBase="ExampleApp"></Context>
Note: defining the context path manually has the side effect that the application is deployed twice by default: at http://localhost:8080/ExampleApp/ as well as at http://localhost:8080/.
To prevent this, we have to set autoDeploy=”false” and deployOnStartup=”false” in the
<Host name="localhost" appBase="webapps" unpackWARs="true"
autoDeploy="false" deployOnStartup="false">
<Context path="" docBase="ExampleApp"></Context>
<!-- Further settings for localhost -->
</Host>
Important: this option is not recommended anymore, since Tomcat 5: it makes context configurations more invasive, since the server.xml file cannot be reloaded without restarting Tomcat.
5. Specifying the Context Path in an App-Specific XML File
To avoid this problem with the server.xml, we’ve got the third option: we’ll set the context path in an application-specific XML file.
Therefore, we have to create a ROOT.xml at $CATALINA_HOME\conf\Catalina\localhost with the following content:
<Context docBase="../deploy/ExampleApp.war"/>
Two points are worth nothing here.
First, we don’t have to specify the path explicitly as in the previous option – Tomcat derives that from the name of our ROOT.xml.
And second – since we’re defining our context in a different file than the server.xml, our docBase has to be outside of $CATALINA_HOME\webApps.
6. Conclusion
In this tutorial, we discussed different options of how to deploy a web application at the root of a Tomcat.