[tomcat] 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.

This question is related to tomcat tomcat7 root context.xml

The answer is


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.


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

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!


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.


Here follows the only solutions that worked for me. Add this to the Host node in the conf/server.xml

<Context path="" docBase="yourAppContextName">

  <!-- Default set of monitored resources -->
  <WatchedResource>WEB-INF/web.xml</WatchedResource>

</Context>

go to Tomcat server.xml file and set path blank


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

go to Tomcat server.xml file and set path blank


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


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

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

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.


This little code worked for me, using virtual hosts

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

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.


Examples related to tomcat

Jersey stopped working with InjectionManagerFactory not found The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat Spring boot: Unable to start embedded Tomcat servlet container Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists Spring Boot application in eclipse, the Tomcat connector configured to listen on port XXXX failed to start Kill tomcat service running on any port, Windows Tomcat 8 is not able to handle get request with '|' in query parameters? 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE 403 Access Denied on Tomcat 8 Manager App without prompting for user/password Difference between Xms and Xmx and XX:MaxPermSize

Examples related to tomcat7

Could not load the Tomcat server configuration INFO: No Spring WebApplicationInitializer types detected on classpath Name [jdbc/mydb] is not bound in this Context I cannot access tomcat admin console? Tomcat 7.0.43 "INFO: Error parsing HTTP request header" jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class Tomcat Server not starting with in 45 seconds HTTP Status 500 - Servlet.init() for servlet Dispatcher threw exception How to fix JSP compiler warning: one JAR was scanned for TLDs yet contained no TLDs? java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

Examples related to root

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' Connect to docker container as user other than root MySQL user DB does not have password columns - Installing MySQL on OSX vagrant login as root by default adb shell su works but adb root does not Android: adbd cannot run as root in production builds How to get domain root url in Laravel 4? Import Certificate to Trusted Root but not to Personal [Command Line] How to check if running as root in a bash script Access Denied for User 'root'@'localhost' (using password: YES) - No Privileges?

Examples related to context.xml

How to set the context path of a web application in Tomcat 7.0 How can I specify system properties in Tomcat configuration on startup?