title: Servlet mapping direct JSP files rather than using a Servlet to manage the URL requests
body: <p>I am redoing the URL mapping structure within my Java web application. I am trying to find the most efficient and proper way to map the servelets and resources to their proper URLs.</p>
  
  <p>There are two strategies that I have been able to create, but I am not sure which is more efficient.</p>
  
  <p><strong>Mapping All urls to one Servlet which Handles the requests</strong></p>
  
  <p>In this case I have a Servlet named "URL", with the following servlet mapping:</p>
  
  <pre><code>&lt;servlet-mapping&gt;
   &lt;servlet-name&gt;url&lt;/servlet-name&gt;
   &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>The url Servlet is set up like such as an example and works fine:</p>
  
  <pre><code>String task = request.getRequestURI().substring(request.getContextPath().length());
  if ("/home".equals(task)){
      RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/home.jsp");
      dispatcher.forward(request, response);
  }
  </code></pre>
  
  <p>The problem I initially had with this that all the static resources such as JS, Images, etc... weren't served. I had the option to create separate directories for the static content as a solution, but off the top of my head I switched to mapping it all directly in the web.xml.</p>
  
  <p><strong>Mapping it all directly in the web.xml.</strong></p>
  
  <p>In this case the url patterns are directly mapped to the JSPs and Servlets like so:</p>
  
  <pre><code>&lt;servlet&gt;
    &lt;servlet-name&gt;home&lt;/servlet-name&gt;
    &lt;jsp-file&gt;/jsp/Home.jsp&lt;/jsp-file&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;home&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/home&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Example:</p>
  
  <pre><code>localhost:8080/home &lt;- home.jsp
  
  localhost:8080/about &lt;- about.jsp
  
  localhost:8080/login &lt;- doLogin servlet
  </code></pre>
  
  <p>Are these bad? Which would be a more efficient and proper way to map the urls to their intended JSP files and Servlets?</p>