173

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.

Lucky
  • 16,787
  • 19
  • 117
  • 151
Chantz
  • 5,883
  • 10
  • 56
  • 79
  • I ever file bug for Tomcat 8 [WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path "/" does not meet these criteria and has been changed to ""](https://bz.apache.org/bugzilla/show_bug.cgi?id=58754) – gavenkoa Dec 19 '15 at 10:59
  • 2
    The reason "It is NOT recommended to place elements directly in the server.xml file" given in the docs follows in the next sentence: "This is because it makes modifying the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat." The accepted answer below directly solves the problem, but if you're not likely to modify this path again or restarts are not too disruptive, modifying the server.xml doesn't seem all that unreasonable. – rimsky Aug 31 '16 at 19:52

14 Answers14

247

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!

kosa
  • 65,990
  • 13
  • 130
  • 167
Paaske
  • 4,345
  • 1
  • 21
  • 33
  • 1
    Will this work with other wars in the regular webapps folder? – chrislovecnm Oct 03 '12 at 19:11
  • 1
    looking at the docs http://tomcat.apache.org/tomcat-7.0-doc/config/context.html docbase is the path/to/yourApp and path must be "" (so an empty string) meaning the root context – Fabio Bonfante Oct 18 '12 at 22:01
  • @chrislovecnm, yes, you can use both methods in parallell. – Paaske Oct 22 '12 at 07:58
  • 6
    To solve the double-deployment you can also set both "deployOnStartup" and "autoDeploy" false of Host attribute in the server.xml – Sefler Jan 10 '13 at 15:05
  • 8
    I have found that if you don't rename the default ROOT folder under /webapps "the cat comes back" and it resets the docBase in the ROOT.xml. This is with VMWare's vfabric tc development server... Be warned. – hoserdude Jan 16 '13 at 20:19
  • 1
    Setting the path attribute is not valid in this case (as per the docs) – Mark Thomas Feb 20 '13 at 21:01
  • I didn't downvote, but solution isn't great: `path` attribute is illegal in a `context.xml` file. – Christopher Schultz Feb 21 '13 at 00:47
  • using of `path=""` where can I find the deployed project? because there is no `webapps/ROOT` directory under tomcat? – tokhi Oct 22 '13 at 15:26
  • Great comment about application loading twice. That will mess up your application if you run things like elasticsearch. – Tommy Sep 29 '14 at 13:09
  • I didn't experience the double deployment issue. My configuration is seen at http://stackoverflow.com/questions/26652264/tomcat-7-application-java-environment-variables-jndi-less/26653238#26653238 – Stephane Oct 30 '14 at 13:52
  • 4
    It appears that Tomcat 7 will not allow a docBase inside /webapps now, so it would appear to be mandatory to locate the war file elsewhere. – Mojo Nov 19 '14 at 00:39
  • 1
    What if I want to use a WAR file instead of an exploded one? Apparently Tomcat refuses to allow this. Any ideas? – Bogdan Zurac Jul 07 '15 at 08:39
  • @Andrew I couldn't have it set up with a war file, ended up defining a ROOT.jar being a symlink to the jar file – CharlesB Aug 12 '15 at 12:25
  • @Mojo seems to be right about tomcat7 and having to move the application outside of webapps directory. Deploying a .war worked for me after I moved it outside of webapps directory. – chris544 Sep 17 '15 at 16:30
  • 1
    `` should be `` – Abdull Jan 26 '16 at 17:30
  • I second the comment by @abdull. Based on [this answer](http://askubuntu.com/a/43229), it should be `` not ``. – Tiz May 26 '16 at 11:13
  • According to the [docs](https://tomcat.apache.org/tomcat-7.0-doc/introduction.html#Directories_and_Files), whether you need to use `` or `` is depending on your installation. If you download from tomcat.apache.org, and only intend to run one instance of Tomcat per server, you'll only need ``. – Paaske May 27 '16 at 07:17
  • I tried the above solution but it's not working for me. When i deploy the app for the 1st time i get a 404 error but when I restart the server then the app loads correctly..Any idea what i'm doing wrong?? – Lucy Oct 27 '16 at 12:30
  • I created `ROOT.xml` in `$CATALINA_HOME/conf/Catalina/localhost` and set `docBase` to `Foobar`, but going to `http://example.com:8080` still shows Tomcat home page instead of my app (assuming `example.com` is my domain and `Foobar` is the name of my webapp). I even tried to set `docBase` to `/opt/tomcat/webapps/Foobar`, but still not working. Of course I restart Tomcat after making changes, but not working. Any suggestions? – kimbaudi Dec 24 '16 at 17:40
  • @kimbaudi [See hoserdude's comment](http://stackoverflow.com/questions/7276989/howto-set-the-context-path-of-a-web-application-in-tomcat-7-0#comment19978282_7706950) and this answer: http://stackoverflow.com/a/3921962/399105 – bmaupin Mar 10 '17 at 20:30
  • Is that means i still need to put the war file under webapps first, start server, let it generate the app folder, shutdown the server, move it outside the webapps and restart server? – gozizibj Jul 02 '17 at 14:44
  • @gozizibj yes, after moving it (unzipped war folder) from webapps to new docBase, it worked. – Kanagavelu Sugumar Jul 03 '18 at 12:53
  • But when i have two Service/application listening on different port, just want to handle only specific port application context. How to achieve that ? Is this ROOT.xml is configured on which port ? – Kanagavelu Sugumar Jul 04 '18 at 06:43
15

This is the the only solution that worked for me. Add the following to the Host node in the conf/server.xml file.

<Context path="" docBase="yourAppContextName">
 
  <!-- Default set of monitored resources -->
  <WatchedResource>WEB-INF/web.xml</WatchedResource>

</Context>

Update:
It can be either in : conf/server.xml
or in : conf/context.xml

smilyface
  • 5,021
  • 8
  • 41
  • 57
rodvlopes
  • 895
  • 1
  • 8
  • 18
6

In Tomcat 9.0, I only have to change the following in the server.xml

<Context docBase="web" path="/web" reloadable="true" source="org.eclipse.jst.jee.server:web"/>

to

<Context docBase="web" path="" reloadable="true" source="org.eclipse.jst.jee.server:web"/>
lmat - Reinstate Monica
  • 7,289
  • 6
  • 48
  • 62
MK Yung
  • 4,344
  • 6
  • 30
  • 35
  • It's working, but it causes double deployment if autoDeploy="true" is set. This is already addressed by the accepted answer. 2nd, it's not recommended way to add Context section in server.xml directly. https://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Defining%20a%20context – thinwa Jan 12 '22 at 04:03
3

This little code worked for me, using virtual hosts

<Host name="my.host.name" >
   <Context path="" docBase="/path/to/myapp.war"/>
</Host>
Jorge Sanchez
  • 1,619
  • 1
  • 13
  • 14
  • This is in `server.xml`? According to e.g. Ali.Mojtehedy above that's problematic. Also, other answers state that `/path/to` needs to be *outside* of the normal webapps path. – Jean Jordaan May 01 '14 at 09:39
  • Seems to work fine. Of course, you don't want the ROOT application to exist in the same webapps folder to conflict with yours. – Anthony Hayward Apr 22 '15 at 13:12
2

Quickest and may be the best solution is to have below content in <TOMCAT_INSTALL_DIR>/conf/Catalina/localhost/ROOT.xml

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

And your webapp will be available at http://<host>:<port>/

  • 1
    Is that docBase directory absolute or relative to a specific folder? – Jose Martinez Feb 17 '15 at 21:35
  • 3
    I tried this, but its not working. I have an app called `Foobar` in `/opt/tomcat/webapps/Foobar`. I tried setting docBase to `Foobar` and also `/opt/tomcat/webapps/Foobar`, but when I go to `http://:/ I still see Tomcat home page instead of my Foobar app. I even restarted Tomcat. What am I doing wrong? – kimbaudi Dec 24 '16 at 17:47
  • When I make the path null it doesn't reach my app ) : – Derrops Mar 15 '19 at 03:03
2

It's not recommended to update the server configuration like server.xml or ROOT.xml.

You can put a context.xml configuration file under your web-application META-INF directory, with the context path setting included. This will override the default server setting?

i.e.:

<Context docBase="yourAppName" path="/yourAppPath" reloadable="true">
Ivan
  • 417
  • 1
  • 4
  • 10
2

<Context docBase="yourAppName" path="" reloadable="true">

go to Tomcat server.xml file and set path blank

Vipin Yadav
  • 707
  • 5
  • 6
2

For me both answers worked.

  1. Adding a file called ROOT.xml in /conf/Catalina/localhost/
<Context
    docBase="/tmp/wars/hpong"
  path=""
  reloadable="true"
/>
  1. Adding entry in server.xml
<Service name="Catalina2">
    <Connector port="8070" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8743" />
    <Engine name="Catalina2" defaultHost="localhost">
        <Host name="localhost"
            unpackWARs="true" autoDeploy="true">
            <Context path="" docBase="/tmp/wars/hpong"  reloadable="true">
                <WatchedResource>WEB-INF/web.xml</WatchedResource>
            </Context>
      </Host>
    </Engine>
</Service>

Note: when you declare docBase under context then ignore appBase at Host.

  1. However I have preferred converting my war name as ROOT.war and place it under webapps. So now unmatched url requests from other wars(contextpaths) will land into this war. This is better way to handle ROOT ("/**") context path.

The second option is (double) loading the wars from Webapps folder as well. Also it only needs uncompressed war folder which is a headache.

Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
1

I faced this problem for one month,Putting context tag inside server.xml is not safe it affect context elements deploying for all other host ,for big apps it take connection errors also not good isolation for example you may access other sites by folder name domain2.com/domain1Folder !! also database session connections loaded twice ! the other way is put ROOT.xml file that has context tag with full path such :

 <Context path="" docBase="/var/lib/tomcat7/webapps/ROOT" />

in conf/catalina/webappsfoldername and deploy war file as ROOT.war inside webappsfoldername and also specify host such

 <Host name="domianname"  appBase="webapps2" unpackWARs="true"  autoDeploy="true"  xmlValidation="false" xmlNamespaceAware="false" >

        <Logger className="org.apache.catalina.logger.FileLogger"
               directory="logs"  prefix="localhost_log." suffix=".txt"
          timestamp="true"/>
</Host>

In this approach also for same type apps user sessions has not good isolation ! you may inside app1 if app1 same as app2 you may after login by server side session automatically can login to app2 ?! So you have to keep users session in client side cache and not with jsessionid ! we may change engine name from localhost to solve it. but let say playing with tomcat need more time than play with other cats!

Ali.Mojtahed
  • 2,537
  • 1
  • 15
  • 23
1

Tomcat 8 : After many searches this is only working code: in server.xml

<!-- Set /apple as default path -->
    <Host name="localhost"  appBase="webapps"
         unpackWARs="true" autoDeploy="true">
     <Context path="" docBase="apple">
         <!-- Default set of monitored resources -->
         <WatchedResource>WEB-INF/web.xml</WatchedResource>
     </Context>
    </Host>

Restart Tomcat, make sure when you access 127.0.0.1:8080, it will display the content in 127.0.0.1:8080/apple

My project was java web application witch created by netbeans ,I set context path in project configuration, no other thing, even I put apple.war in webapps folder.

MrSalesi
  • 377
  • 3
  • 17
1

In Tomcat 8.X ,under tomcat home directory /conf/ folder in server.xml you can add <Context> tag under <Host> tag as shown below . But you have to restart the server in order to take effect

  <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">

     <Context docBase="${catalina.base}\webapps\<Your App Directory Name>" path="<your app path you wish>" reloadable="true" />
  </Host>

OR if you are using Tomcat 7.X you can add context.xml file in WEB-INF folder in your project . The contents of the file i used is as shown . and it worked fine for me . you don't have to restart server in this case .

<?xml version="1.0" encoding="UTF-8"?>

<Context docBase="${catalina.base}\webapps\<My App Directory Name>" path="<your app path you wish>" reloadable="true" />
harsha kumar Reddy
  • 1,251
  • 1
  • 20
  • 32
  • Tested in tomcat:9.0 docker and it works as 8.0 you mentioned. `localhost:8080//` and `localhost:8080//` both of these 2 URLs works at the same time. Found these two extracted folders under `$CATALINA_HOME/webapps/`. – niaomingjian Jul 14 '20 at 13:38
1

Simplest and flexible solution is below: Inside ${Tomcat_home}/config/server.xml

Change the autoDeploy="false" deployOnStartup="false" under Host element like below This is must.

<Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="false" deployOnStartup="false">

Add below line under Host element.

<Context path="" docBase="ServletInAction.war"  reloadable="true">
            <WatchedResource>WEB-INF/web.xml</WatchedResource>
        </Context>

With the above approach we can add as many applications under webapps with different context path names.

Ravi Gupta
  • 81
  • 5
  • Alternate solution without doinf above configuration is just rename your war file to root.war and put it under webapps directory. Automatically context path will be set to /. – Ravi Gupta May 23 '19 at 06:22
0

When it comes to run tomcat in a container (https://hub.docker.com/_/tomcat) the most convenient approch is to mount the war file as ROOT.war in /usr/local/tomcat/webapps/. Modifieing server.xml or context.xml is much more complicated.

xtermi2
  • 470
  • 5
  • 10
-6

The below trick worked for me.

1) Comment/delete the below configuration from server.xml file (inside conf folder) of tomcat.

2) Delete the existing ROOT folder (If any) residing inside tomcat webapps folder. And rename your war (e.g: test.war ) file to ROOT.war.

Remember that while renaming war file to ROOT.war "ROOT" should be in caps.

Limitation: You can deploy only one application inside one tomcat instance.

  • 3
    From the question: "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." – Jean Jordaan May 01 '14 at 09:35
  • 1
    I would not like to restrict myself to just using a single webapp per tomcat instance. – ulrich Apr 02 '15 at 09:13