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

541,032

Solution 1

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!

Solution 2

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

Solution 3

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"/>

Solution 4

This little code worked for me, using virtual hosts

<Host name="my.host.name" >
   <Context path="" docBase="/path/to/myapp.war"/>
</Host>

Solution 5

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">
Share:
541,032

Related videos on Youtube

Chantz
Author by

Chantz

Updated on July 08, 2022

Comments

  • Chantz
    Chantz almost 2 years

    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.

  • chrislovecnm
    chrislovecnm over 11 years
    Will this work with other wars in the regular webapps folder?
  • Fabio Bonfante
    Fabio Bonfante over 11 years
    looking at the docs 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
  • Paaske
    Paaske over 11 years
    @chrislovecnm, yes, you can use both methods in parallell.
  • Sefler
    Sefler over 11 years
    To solve the double-deployment you can also set both "deployOnStartup" and "autoDeploy" false of Host attribute in the server.xml
  • hoserdude
    hoserdude over 11 years
    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.
  • Mark Thomas
    Mark Thomas about 11 years
    Setting the path attribute is not valid in this case (as per the docs)
  • Christopher Schultz
    Christopher Schultz about 11 years
    I didn't downvote, but solution isn't great: path attribute is illegal in a context.xml file.
  • tokhi
    tokhi over 10 years
    using of path="" where can I find the deployed project? because there is no webapps/ROOT directory under tomcat?
  • Jean Jordaan
    Jean Jordaan almost 10 years
    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
    Jean Jordaan almost 10 years
    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.
  • Tommy
    Tommy over 9 years
    Great comment about application loading twice. That will mess up your application if you run things like elasticsearch.
  • Saif
    Saif over 9 years
    will be at server.xml or context.xml? i see an xml tag <Context> in my context.xml
  • Ismail Yavuz
    Ismail Yavuz over 9 years
    There is also a context xml tag in server.xml However server.xml usage for context defining is discouraged
  • Stephane
    Stephane over 9 years
    I didn't experience the double deployment issue. My configuration is seen at stackoverflow.com/questions/26652264/…
  • Mojo
    Mojo over 9 years
    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.
  • Jose Martinez
    Jose Martinez about 9 years
    Is that docBase directory absolute or relative to a specific folder?
  • ulrich
    ulrich about 9 years
    I would not like to restrict myself to just using a single webapp per tomcat instance.
  • Anthony Hayward
    Anthony Hayward about 9 years
    Seems to work fine. Of course, you don't want the ROOT application to exist in the same webapps folder to conflict with yours.
  • Bogdan Zurac
    Bogdan Zurac almost 9 years
    What if I want to use a WAR file instead of an exploded one? Apparently Tomcat refuses to allow this. Any ideas?
  • CharlesB
    CharlesB over 8 years
    @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
  • chris544
    chris544 over 8 years
    @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.
  • Abdull
    Abdull about 8 years
    <catalina_home> should be <catalina_base>
  • Tiz
    Tiz almost 8 years
    I second the comment by @abdull. Based on this answer, it should be <catalina_base> not <catalina_home>.
  • Paaske
    Paaske almost 8 years
    According to the docs, whether you need to use <catalina_home> or <catalina_base> 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 <catalina_home>.
  • Lucy
    Lucy over 7 years
    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??
  • kimbaudi
    kimbaudi over 7 years
    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
    kimbaudi over 7 years
    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://<host>:<port>/ I still see Tomcat home page instead of my Foobar app. I even restarted Tomcat. What am I doing wrong?
  • bmaupin
    bmaupin about 7 years
  • gozizibj
    gozizibj almost 7 years
    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?
  • Kanagavelu Sugumar
    Kanagavelu Sugumar almost 6 years
    @gozizibj yes, after moving it (unzipped war folder) from webapps to new docBase, it worked.
  • Kanagavelu Sugumar
    Kanagavelu Sugumar almost 6 years
    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
    Kanagavelu Sugumar almost 6 years
  • Kanagavelu Sugumar
    Kanagavelu Sugumar almost 6 years
  • Derrops
    Derrops about 5 years
    When I make the path null it doesn't reach my app ) :
  • Ravi Gupta
    Ravi Gupta almost 5 years
    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 /.
  • niaomingjian
    niaomingjian almost 4 years
    Tested in tomcat:9.0 docker and it works as 8.0 you mentioned. localhost:8080/<Your App Directory Name>/ and localhost:8080/<your app path you wish>/ both of these 2 URLs works at the same time. Found these two extracted folders under $CATALINA_HOME/webapps/.
  • thinwa
    thinwa over 2 years
    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. tomcat.apache.org/tomcat-9.0-doc/config/…