Explain Codes LogoExplain Codes Logo

Deploying my application at the root in Tomcat

java
deployment
tomcat
server.xml
Nikita BarsukovbyNikita Barsukov·Feb 4, 2025
TLDR

To deploy your app at Tomcat's root, simply rename your .war to ROOT.war and place it right inside the webapps/ directory. First, you must get rid of any existing ROOT/ directory. Your app will then pop up at http://hostname:port/ - Voila!

mv your-app.war /path/to/tomcat/webapps/ROOT.war // #Renaming, step 1! rm -rf /path/to/tomcat/webapps/ROOT/ // #Sweeping the old stuff away sh /path/to/tomcat/bin/startup.sh // #Ready, Steady, Go!

Just like magic, your app now lives at the server's base path, and you didn't even have to move a muscle with extra context.

Drill down: Details & stumbling blocks

Configuring server.xml: Your conductor's baton

Order and precision is the game while establishing the <Context> within the <Host> section of the server.xml, like a symphony conductor orchestrating a masterpiece.

<Context path="" docBase="your-app-name" reloadable="true" /> // #Beethoven's Fifth Symphony of app deployment

How about conducting a larger orchestra? Define comprehensive <Context> in a separate your-app.xml inside conf/Catalina/localhost/ for a clearer management of your ensemble.

Multiple apps? Piece of cake!

Deploying multiple applications at root on different ports or IP addresses? With separate <Service> tags per app in your server.xml, you'll be juggling them like a pro. Just remember to tweak the Connector settings for each.

Security: Prioritize!

For the VIP apps needing an extra layer of security, add privileged="true" inside the <Context> element. Tomcat has got your back!

Manual over convenience: You call the shots!

If you're someone who likes to have manual control over the deployment process, you can set autoDeploy and deployOnStartup to false in your server.xml:

<Host name="localhost" appBase="webapps" autoDeploy="false" deployOnStartup="false"> // #Captain of my ship, master of my deployment!

Slips & trips: Avoid those!

Tripped over a duplicate deployment causing conflicts? Double-check before you leap! Also, keep an eye on the compatibility of your war file with your Tomcat version. Last but not least, restart Tomcat whenever you tweak the server.xml to make sure the changes stick.

Extra tuning and deep cuts

Unpacked deployment: The rough cut

In the development stage, using unpacked deployments or exploded WAR directories enables hot-reloading. You can see changes in real-time without going through a tortuous deployment cycle.

WatchedResource: The watchful roadie

Think of WatchedResource tags as your roadies, always on the lookout for changes and ready to reload contexts when those changes happen.

<Context ...> <WatchedResource>WEB-INF/web.xml</WatchedResource> // #Where's my roadie? </Context>

Special tricks for Tomcat 7

Always keep an eye out for version-specific settings, especially if you're working with Tomcat 7 or older. It's like knowing your way around the quirks of an older guitar model.

Syntax & formatting: Score perfection

Every note counts in a music score; similarly, every character matters in XML files like server.xml. After making changes, always validate your XML to avoid flat notes!