Search This Blog

Thursday 7 March 2013

ROOT.war

Deploying a web application in Tomcat involves placing the war file or the extracted folder in Tomcat's webapps folder. I created a very basic web application named simple and placed it in the webapps folder.

I deployed it to my local tomcat instance on port 80. I also renamed my machine to "www.robin.com". So the application URL is  http://www.robin.com/simple.
On hitting the URL, the welcome page index.jsp is displayed.
There is just one slight eyesore. All my URLs include the name of the webapp. Tomcat provides us with a nifty trick to overcome this problem.
We can avoid the web app name being displayed by renaming our war as "ROOT.war". Or our extracted folder as ROOT. In this case our application is now the root application. A request to the Tomcat instance could now directly resolve onto our application.

This will also work for all relative URLs - basically any URL without the webapp name hard-coded in it. If we do a full deployment of Tomcat, we can already find the welcome application deployed with the ROOT name. That is why directly accessing Tomcat guides us to the same.
Important thing to note is that we cannot access the same URL with the name ROOT. So the below would result in an error:
So while you can avoid using webapp names in your URL if you name your webapp as "ROOT", you cannot access any URL using the "ROOT" webapp.
I don't think any of the big websites directly uses Tomcat as the web front end. But for small applications with Tomcat deployed as the web layer we could achieve a nice looking URL with this trick

4 comments:

  1. Or just configure path in server.xml for your app..



    see http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Context_Parameters

    ReplyDelete
  2. How did you renamed my machine to "www.robin.com"?
    Thank you

    ReplyDelete
    Replies
    1. I added an entry in my hosts file - not a portable solution, just to make the example clearer.

      Delete