I want to deploy myapp in tomcat server. I know one way is to delete the Root folder from webapps and rename my app.war as ROOT.WAR. But i do not want to do this. I want to deploy like when user access my app as www.xxx.com , it should go to my app like www.xxx.com/login.html. Please help
-
1Possible duplicate of [Deploying my application at the root in Tomcat](http://stackoverflow.com/questions/5328518/deploying-my-application-at-the-root-in-tomcat) – Christopher Schultz Dec 21 '16 at 22:49
3 Answers
You can deploy any WAR file (or exploded WAR directory) from (almost) anywhere with any context name you want, as long as it doesn't conflict with any auto-deployed application. Here's how:
Remove any conflicting applications from
webapps/
(in this case, really, it's justROOT
)Move your own application someplace other than in
webapps/
Copy the
META-INF/context.xml
file from your application and place it into Tomcat'sconf/[engine]/[host]/[contextpath].xml
– for exampleconf/Catalina/localhost/ROOT.xml
Edit
conf/[engine]/[host]/[contextpath].xml
and add this attribute to the<Context>
element:docBase="/path/to/your/WAR.war"
Now, Tomcat will deploy /path/to/your/WAR.war
on context path /
(because the deployment descriptor's filename is ROOT
), and you didn't have to modify conf/server.xml
.
Who cares about modifying conf/server.xml
? Well, that file is only read once during container startup, so if you want to make changes, undeploy, redeploy, etc. you must restart the entire container to pick-up those changes. It's much better to use Tomcat's auto-deployment facility, which as you can see is quite flexible.
All of this is laid out in Tomcat's documentation on defining contexts where, incidentally, the very first sentence makes it clear that defining a context in server.xml
is a bad practice.

- 1
- 1

- 20,221
- 9
- 60
- 77
-
I know this is an old answer, but even after doing this, my app is deployed at `localhost:port/myapp` instead of `localhost:port/`. This is on Tomcat 9.0.20. I am deleting ROOT from webapps, copying `myapp.war` to tomcat/app which is a custom location, copying `myapp.xml` to `/usr/local/tomcat/conf/Catalina/localhost/` which contains `
`. Can you help me figure out what I am doing wrong? – Sajib Acharya Jun 08 '19 at 23:53 -
2@SajibAcharya You need to name your XML file `ROOT.xml` instead of `myapp.xml`. Tomcat derives the context path from the name of the XML file, and the name `ROOT.xml` is treated specially by using a zero-length string as the context-path. – Christopher Schultz Jun 09 '19 at 01:54
-
I tried this and after I finish the steps and deploy my war, then 2 new apps show up under Tomcat's Web App Manager list page, one named the original like "myapp" and one "/" for root. However the root app is not started and doesn't work. The "myapp" one works as usual. I tried adjusting the path in ROOT.xml but no luck. Also undeploying deletes the ROOT.xml. I'll try reading that Tomcat page but may need to go back to just renaming to ROOT.war every time which seems strange but works well. – Michael K Mar 02 '20 at 22:59
-
If you put your WAR file into Tomcat's auto-deployment `webapps` directory, then it will be deployed twice if you use the method detailed in this answer. Note in step 2 it says *Move your own application someplace other than in `webapps/`*. – Christopher Schultz Apr 10 '20 at 20:18
-
@ChristopherSchultz could you elaborate why the solution proposed by you is better than just placing ROOT.war in the `webapps` folder? Using ROOT.war is. by far, much simpler so it would be very helpful to understand why we should favor much more complex solution described above. – Jacek J Jan 02 '22 at 22:28
You have several options to set a root application. (see https://stackoverflow.com/a/5328636/6371459)
Remove
ROOT
directory and deploy your war asROOT.war
Deploy your war in
webapps
and configure the context root inconf/server.xml
to use your war file (not recommended because requires restarting)Create a specific context file
$CATALINA_BASE/conf/[enginename]/[hostname]/ROOT.xml
and setdocBase=your_war_name
like previous2)
(see @ChristopherShultz answer)
Since you have discarded option 1, and option 2 is not recommended, I suggest use a context file
-
1
-
Why @ChristopherSchultz? I could agree but there are not many options if the OP does not want to rename the war to ROOT. If you think this option is not applicable, I think you should include your own answer with the other option of the link instead of marking it as duplicate. 5 of the 6 upvoted answers provide the same tecnhique, and only one negative comment – pedrofb Dec 22 '16 at 06:14
Configuration file is server.xml
You can just follow this example : https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-server-xml-configuration-example/
referencing tomcat documentation : http://tomcat.apache.org/tomcat-7.0-doc/config/context.html

- 218
- 2
- 11