title: Servlets on Tomcat: Requested resource is not found
body: <p>I am on Mac OSX. This is my first Servlet. I have Tomcat 8.0.14  </p>
  
  <p>I have created a Servlet that simply takes three parameters via HTTP POST method. This 
  is my Servlet: </p>
  
  <p><strong><em>HelloWorld.java:</em></strong></p>
  
  <pre><code>package com.hello.world;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  import java.io.PrintWriter;
  
  public class HelloWorld extends HttpServlet {
  
      @Override
      public void doPost(HttpServletRequest request, HttpServletResponse response) {
  
          String fname = request.getParameter("fname");
          String mname = request.getParameter("mname");
          String lname = request.getParameter("lname");
  
          response.setContentType("text/html");
  
          try (PrintWriter out = response.getWriter()) {
  
              out.println("&lt;center&gt;&lt;h1&gt;HelloWorld!&lt;br&gt;&lt;/h1&gt;");
              out.println("&lt;h2&gt;" + (fname != null ? fname : "") + " "
                                 + (mname != null ? mname : "") + " "
                                 + (lname != null ? lname : "") + "&lt;/h2&gt;&lt;/center&gt;");
  
          } catch (IOException exception) {
              exception.printStackTrace();
          }
      }
  }  
  </code></pre>
  
  <p>The code compiles properly and I have placed the compiled class file in the default <code>webapps/ROOT/WEB-INF/classes</code> directory as:  </p>
  
  <pre class="lang-none prettyprint-override"><code>/Library/Java/Tomcat/webapps/ROOT/WEB-INF/classes/com/hello/world/HelloWorld.class  
  </code></pre>
  
  <p>Then I have my <strong><em>web.xml</em></strong> file as:  </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  
  &lt;web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                                http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
           version="3.1"
           metadata-complete="true"&gt;
  
           &lt;servlet&gt;
               &lt;servlet-name&gt;HelloWorld&lt;/servlet-name&gt;
               &lt;servlet-class&gt;com.hello.world.HelloWorld&lt;/servlet-class&gt;
           &lt;/servlet&gt;
  
           &lt;servlet-mapping&gt;
               &lt;servlet-name&gt;HelloWorld&lt;/servlet-name&gt;
               &lt;url-pattern&gt;/controller.do&lt;/url-pattern&gt;
           &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;   
  </code></pre>
  
  <p>I have placed this file in the default <code>webapps/ROOT/WEB-INF/</code> directory as:  </p>
  
  <pre class="lang-none prettyprint-override"><code>/Library/Java/Tomcat/webapps/ROOT/WEB-INF/web.xml
  </code></pre>
  
  <p>Then I have my <code>index.html</code> as:  </p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Hello, World!&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;form method="POST" action="controller.do"&gt;
              &lt;center&gt;
                  &lt;table&gt;
                      &lt;tr align="right"&gt;
                          &lt;td&gt;&lt;label for="fname"&gt;Firstname:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="textfield" name="fname"&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr align="right"&gt;
                          &lt;td&gt;&lt;label for="mname"&gt;Middlename:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="textfield" name="mname"&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr align="right"&gt;
                          &lt;td&gt;&lt;label for="lname"&gt;Lastname:&lt;/label&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="textfield" name="lname"&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
  
                  &lt;input type="submit" value="Post this stupid form"&gt;
              &lt;/center&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I have placed this file in the default <code>webapps/ROOT/</code> directory as:  </p>
  
  <pre class="lang-none prettyprint-override"><code>/Library/Java/Tomcat/webapps/ROOT/index.html
  </code></pre>
  
  <p>When I type <code>localhost:8080/index.html</code>, the HTML loads up and I am able to post the form properly and get the desired output.<br>
  This works fine.  </p>
  
  <p>But when I create a separate directory named <code>HelloWorld</code> and move these three files in it, such than my directory structure becomes like this:  </p>
  
  <pre class="lang-none prettyprint-override"><code>/Library/Tomcat/webapps/ROOT
  |
  |-&gt;HelloWorld
      |
      |-&gt;index.html
      |-&gt;WEB-INF
          | 
          |-&gt;web.xml
          |-&gt;classes
              |
              |-&gt;com
                  |-&gt;hello
                      |-&gt;world
                          |-&gt;HelloWorld.class  
  </code></pre>
  
  <p>and type <code>localhost:8080/HelloWorld</code>, the HTML loads up fine. But when I post the form, it says </p>
  
  <pre class="lang-none prettyprint-override"><code>HTTP Status 404 - /HelloWorld/controller.do
  
  type Status report
  
  message /HelloWorld/controller.do
  
  description The requested resource is not available.
  
  Apache Tomcat/8.0.14  
  </code></pre>
  
  <p>I am not able to figure out what is the problem.  Is it because I have created a separate directory? But I think that shouldn't be. I need help. How do I get this working?</p>