title: How to set Spring root context path
body: <p>I am a Spring newbie and am putting together a Spring web-app (not Spring-boot - how much difference does this make?).  Deployment is on a Tomcat 7 server.</p>
  
  <p>The app is up and running.  My problem is that is only accessible via the standard URL:</p>
  
  <p><a href="http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/index.html" rel="nofollow noreferrer">http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/index.html</a></p>
  
  <p>The following do not work:
  <a href="http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT" rel="nofollow noreferrer">http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT</a>
  <a href="http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/" rel="nofollow noreferrer">http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/</a></p>
  
  <p>even though my web.xml lists index.html as the welcome page.</p>
  
  <p>An additional symptom of the problem is that all sorts of links within the application need to be specified as relative, rather than with a prepended '/'.</p>
  
  <p>But even these urls are not what I really want.</p>
  
  <p>I would rather specify</p>
  
  <p><a href="http://mycompany.com:8081/cwing" rel="nofollow noreferrer">http://mycompany.com:8081/cwing</a></p>
  
  <p>and have it bring up the index.html.</p>
  
  <p>Following the lead of <a href="https://stackoverflow.com/questions/20405474/spring-boot-context-root">Add context path to Spring Boot application</a></p>
  
  <p>I created an application.xml file in src/main/resources with the following content:</p>
  
  <pre><code>server.contextPath=/cwing
  server.port=8081
  </code></pre>
  
  <p>This doesn't work.  <a href="http://mycompany.com:8081/cwing" rel="nofollow noreferrer">http://mycompany.com:8081/cwing</a> brings up a blank page, with a 404 error on the server, as does <a href="http://mycompany.com:8081/cwing/index.html" rel="nofollow noreferrer">http://mycompany.com:8081/cwing/index.html</a>.</p>
  
  <p>I must be missing something.  What else is needed to get this to work as I want it to work?</p>
  
  <p>UPDATE: as asked, here is the web.xml (irrelevant details removed).</p>
  
  <pre><code>    &lt;?xml version="1.0"?&gt;
      &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
                version="3.0"&gt;
        &lt;display-name&gt;cwing&lt;/display-name&gt;
  
          &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
          &lt;listener&gt;
              &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
          &lt;/listener&gt;
          &lt;!-- The definition of the Root Spring Container shared by all Servlets 
              and Filters --&gt;
          &lt;context-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;
              /WEB-INF/spring/context.xml
              /WEB-INF/spring/datasource.xml
              /WEB-INF/spring/security.xml
              &lt;/param-value&gt;
          &lt;/context-param&gt;
  
          &lt;servlet&gt;
              &lt;servlet-name&gt;d&lt;/servlet-name&gt;
              &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
              &lt;init-param&gt;
                  &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
                  &lt;param-value&gt;
                      /WEB-INF/spring/context.xml
                      /WEB-INF/spring/datasource.xml
                      /WEB-INF/spring/security.xml
                  &lt;/param-value&gt;
              &lt;/init-param&gt;
              &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
              &lt;async-supported&gt;true&lt;/async-supported&gt;
          &lt;/servlet&gt;
  
          &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;d&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
          &lt;/servlet-mapping&gt;
  
          &lt;!-- Disables Servlet Container welcome file handling. Needed for compatibility 
              with Servlet 3.0 and Tomcat 7.0 --&gt;
          &lt;welcome-file-list&gt;
              &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
          &lt;/welcome-file-list&gt;
  
          &lt;filter&gt;
              &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
              &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
          &lt;/filter&gt;
  
          &lt;filter-mapping&gt;
              &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
              &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
          &lt;/filter-mapping&gt; 
  ...
  &lt;/web-app&gt;
  </code></pre>