How to set the context path of a web application in Tomcat 7.0
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">
Related videos on Youtube
Chantz
Updated on July 08, 2022Comments
-
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.
-
gavenkoa over 8 years
-
rimsky over 7 yearsThe 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.
-
-
chrislovecnm over 11 yearsWill this work with other wars in the regular webapps folder?
-
Fabio Bonfante over 11 yearslooking 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 over 11 years@chrislovecnm, yes, you can use both methods in parallell.
-
Sefler over 11 yearsTo solve the double-deployment you can also set both "deployOnStartup" and "autoDeploy" false of Host attribute in the server.xml
-
hoserdude over 11 yearsI 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 about 11 yearsSetting the path attribute is not valid in this case (as per the docs)
-
Christopher Schultz about 11 yearsI didn't downvote, but solution isn't great:
path
attribute is illegal in acontext.xml
file. -
tokhi over 10 yearsusing of
path=""
where can I find the deployed project? because there is nowebapps/ROOT
directory under tomcat? -
Jean Jordaan almost 10 yearsFrom 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 almost 10 yearsThis 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 over 9 yearsGreat comment about application loading twice. That will mess up your application if you run things like elasticsearch.
-
Saif over 9 yearswill be at
server.xml
orcontext.xml
? i see an xml tag<Context>
in mycontext.xml
-
Ismail Yavuz over 9 yearsThere is also a context xml tag in server.xml However server.xml usage for context defining is discouraged
-
Stephane over 9 yearsI didn't experience the double deployment issue. My configuration is seen at stackoverflow.com/questions/26652264/…
-
Mojo over 9 yearsIt 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 about 9 yearsIs that docBase directory absolute or relative to a specific folder?
-
ulrich about 9 yearsI would not like to restrict myself to just using a single webapp per tomcat instance.
-
Anthony Hayward about 9 yearsSeems 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 almost 9 yearsWhat if I want to use a WAR file instead of an exploded one? Apparently Tomcat refuses to allow this. Any ideas?
-
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 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 about 8 years
<catalina_home>
should be<catalina_base>
-
Tiz almost 8 yearsI second the comment by @abdull. Based on this answer, it should be
<catalina_base>
not<catalina_home>
. -
Paaske almost 8 yearsAccording 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 over 7 yearsI 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 over 7 yearsI created
ROOT.xml
in$CATALINA_HOME/conf/Catalina/localhost
and setdocBase
toFoobar
, but going tohttp://example.com:8080
still shows Tomcat home page instead of my app (assumingexample.com
is my domain andFoobar
is the name of my webapp). I even tried to setdocBase
to/opt/tomcat/webapps/Foobar
, but still not working. Of course I restart Tomcat after making changes, but not working. Any suggestions? -
kimbaudi over 7 yearsI tried this, but its not working. I have an app called
Foobar
in/opt/tomcat/webapps/Foobar
. I tried setting docBase toFoobar
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 about 7 years@kimbaudi See hoserdude's comment and this answer: stackoverflow.com/a/3921962/399105
-
gozizibj almost 7 yearsIs 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 almost 6 years@gozizibj yes, after moving it (unzipped war folder) from webapps to new docBase, it worked.
-
Kanagavelu Sugumar almost 6 yearsBut 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 almost 6 years
-
Kanagavelu Sugumar almost 6 years
-
Derrops about 5 yearsWhen I make the path null it doesn't reach my app ) :
-
Ravi Gupta almost 5 yearsAlternate 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 almost 4 yearsTested in tomcat:9.0 docker and it works as 8.0 you mentioned.
localhost:8080/<Your App Directory Name>/
andlocalhost: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 over 2 yearsIt'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/…