title: Context root, virtual path mapping to actual file path, and meaning of leading slash in Servlet
body: <p>After reading lots of other SO answers and also some online articles, I'm trying to summarize the concepts of context root, virtual path to actual file path mapping and meaning of leading slash.</p>
  <ol>
  <li><p>Context root, based on servlet spec, is corresponding to URI <code>http://host:port/&lt;context-root&gt;/</code>. Once our web app is deployed, this context root always maps to the base directory of our web application (e.g. exploded war) in the actual file system, which is always the parent directory of WEB-INF folder.</p>
  </li>
  <li><p>Every time we use leading slash &quot;/&quot; within the web application (e.g. web.xml, servlets, filters), the leading slash is always pointing to the context root,instead of absolute root <code>http://host:port/</code>
  so whenever we do</p>
  <pre class="lang-java prettyprint-override"><code>request.getRequestDispatcher(&quot;/foo/bar/baz.jsp&quot;).forward(...)
  </code></pre>
  <p>From the perspective of servlet container, we are requested a resource located at
  <code>http://host:port/&lt;context-path&gt;/foo/bar/baz.jsp</code>, which maps to the following path
  in file system</p>
  <pre><code>file:///&lt;$CATALINA_HOME&gt;/webapps/our-application/foo/bar/baz.jsp
  </code></pre>
  </li>
  <li><p>Every time we use leading slash &quot;/&quot; in a context that is only meaningful to the client (i.e. browser), the leading slash is always pointing to the absolute root <code>http://host:port/</code>. Examples of places that I can think of is those <code>src</code> and <code>href</code> attribute of html element in JSP files, or redirect URL in <code>HttpServletResponse#sendRedirect</code>, or path in cookie <code>Cookie#setPath</code></p>
  </li>
  </ol>
  <p>Edit: sorry if it is not clear enough: I was looking for someone to correct my understanding if any of them is incorrect or missing something.</p>