title: Mapping a specific servlet to be the default servlet in Tomcat
body: <p>I am trying to implement a servlet that gets raw requests, and decide either to process them, or forward them to another backend server. It is similar to a load-balancer, where a received request is forwarded to one of the (in my case 2) destinations. One of the destination is remote (on another host). Furthermore, the requests could come to the root (<a href="http://mycompany.com/" rel="nofollow noreferrer">http://mycompany.com/</a>).</p>
  
  <p>Since I want to get raw requests, I implemented my own servlet (subclassing <code>HttpServlet</code>), and that works great. My servlet looks like:
  </p>
  
  <pre><code>public class MyProxyServlet extends HttpServlet {
      @Override
      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
          processOrForward(req, resp);
      }
      // also doGet(), doHead(), ...
  }
  </code></pre>
  
  <p>Since the service I want to process may send requests to the root, I would like to map my servlet to be the default servlet, thereby receiving any request that does not have an explicit servlet mapping. Assume my servlet's name is "myservlet", and is running along side of another servlet "foo", I expect all requests in the form of <a href="http://mycompany.com/foo/" rel="nofollow noreferrer">http://mycompany.com/foo/</a>... to be delivered to foo, and everything else (e.g., /, /bar/..., /myservlet/...) to "myservlet". Looking at earlier posts (eg., root mapping <a href="https://stackoverflow.com/questions/4177047/how-to-set-up-the-root-servlet-in-tomcat-6">here</a> and <a href="https://stackoverflow.com/questions/7159902/servlet-mapping">here</a>, or url rewriting <a href="https://stackoverflow.com/questions/10427347/tomcat-url-mapping-individual-servlet-to-root-level">here</a>), I thought I figured it out, but it does not work. </p>
  
  <p>Here is my <em>web.xml</em>:</p>
  
  <pre><code>&lt;web-app&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;ProxyServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.mycompany.MyProxyServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
  
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;ProxyServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>In the above <em>web.xml</em>, for url-pattern I tried </p>
  
  <pre><code>"/" and "/*" and empty (i.e., &lt;url-pattern&gt;&lt;/url-pattern&gt;), all behave the same --&gt;
      Requests to root (/)goes to tomcat's default servlet
      Requests to /myservlet/... are handled by "myservlet"
      Requests to /fubar/... are always 404
  </code></pre>
  
  <p>Is there a way of turning my servlet to be the default. I.e., any request that does not map specifically to a servlet comes to mine (it is even acceptable to receive all requests, since I can deploy this servlet in its own container). In case it matters, I am using Tomcat 7.0.30 on Ubuntu 12.10.</p>