Search code examples
tomcattomcat7rootcontext.xml

How to set the context path of a web application in Tomcat 7.0


I know that I can rename my webapp (or it's WAR file) to ROOT but this is a terrible way to do it, IMHO. Now I checked out the tomcat doc & it says

It is NOT recommended to place elements directly in the server.xml file

So I tried doing it another method that it suggested.

Individual Context elements may be explicitly defined: In an individual file at /META-INF/context.xml inside the application files.

So I created a /META-INF/context.xml with the following code,

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/"/>

But after deploying when I restarted the server it still failed to load the context at "/", it still loaded it with the "/<WEB_APP_NAME>"

Any pointers helpful.


Solution

  • What you can do is the following;

    Add a file called ROOT.xml in <catalina_home>/conf/Catalina/localhost/

    This ROOT.xml will override the default settings for the root context of the tomcat installation for that engine and host (Catalina and localhost).

    Enter the following to the ROOT.xml file;

    <Context 
      docBase="<yourApp>" 
      path="" 
      reloadable="true" 
    />
    

    Here, <yourApp> is the name of, well, your app.. :)

    And there you go, your application is now the default application and will show up on http://localhost:8080

    However, there is one side effect; your application will be loaded twice. Once for localhost:8080 and once for localhost:8080/yourApp. To fix this you can put your application OUTSIDE <catalina_home>/webapps and use a relative or absolute path in the ROOT.xml's docBase tag. Something like this;

    <Context 
      docBase="/opt/mywebapps/<yourApp>" 
      path="" 
      reloadable="true" 
    />
    

    And then it should be all OK!