Tomcat - How to set web application context path as server root
- Details
- Written by Nam Ha Minh
- Last Updated on 06 August 2019   |   Print Email
In this article, we'll guide you how to configure Tomcat server to specify the server's context root is also the context path of a web application. In other words, you will learn how to deploy a Java web application which can be accessed via server's context root - or hostname.
By default, when you deploy a Java web application on Apache Tomcat server, the website can be accessed through this kind of URL :
http://hostname:port/WebAppName/
Here, the context root of the server is hostname, and the context path of the web applicaton is WebAppName.
However, in practice we may want to use only the server's host name to access the web application, via this kind of URL:
http://hostname:port/
In this case the context path of the web application is united with the server root - it would be very convenient for many cases. So, how to setup Tomcat server to change the default?
The simplest way is to deploy your Java web application as ROOT.war under the webapps directory.
Follow these steps:
- Rename your web application's WAR file to ROOT.war
- Make sure the server is stopped.
- Navigate to the webapps directory under Tomcat installation location. This is the base directory of localhost, where Java applications are deployed to.
- Inside webapps, you can see the 3 folders installed by default: docs, manager, and ROOT. docs contains Tomcat documentation; manager is the Tomcat manager application; and ROOT contains welcome pages you see when accessing the server's context root at http://localhost
- Rename or delete the default ROOT directory.
- Copy your ROOT.war file to webapps directory.
- Start Tomcat, and you will see it extracts your ROOT.war file to deploy your web application.
- Now your web application can be accessed via server hostname (or its context root). Enjoy!
NOTES:
You can still access Tomcat manager application with the context path /manager.
Apply the same steps in case you use virtual host with different base directory.
Other Tomcat Tutorials:
- How to deploy a Java web application on Tomcat
- How to Embed Tomcat Server into Java Web Applications
- How to Use Virtual Hosts in Tomcat
- How to configure JNDI DataSource for Database Connection Pooling in Tomcat
- How to configure session timeout in Tomcat
- How to change Tomcat port number
- How to add Tomcat server in Eclipse
- How to change server location and deploy path for Tomcat in Eclipse
Comments