title: Java JSP/Servlet: controller servlet throwing the famous stack overflow
body: <p>I've read several docs and I don't get it: I know I'm doing something wrong but I don't understand what.  I've got a website that is entirely dynamically generated: there's hardly any static content at all.</p>
  
  <p>So, trying to understand JSP/Servlet, I've written my own "front controller" intercepting every single query, it looks like this:  </p>
  
  <pre><code>&lt;servlet-mapping&gt;
          &lt;servlet-name&gt;defaultservlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Basically I want any user request, like:</p>
  
  <ul>
  <li>example.org</li>
  <li>example.org/bar</li>
  <li>example.org/foo.html</li>
  </ul>
  
  <p>to all go through a default servlet which I've written.</p>
  
  <p>The servlet then examines the URI and find to which <em>.jsp</em> the request must be dispatched, and then does, after having set all the attributes correctly, a:</p>
  
  <pre><code>RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp");
  dispatcher.forward(req, resp);
  </code></pre>
  
  <p>When I'm using a url-pattern (in <em>web.xml</em>) like, say, <code>*.html</code>, everything works fine.  But when I change it to <code>/*</code> (to really intercept everything), I enter an endless loop and it ends up with a... StackOverflow :)</p>
  
  <p>When the request is dispatched, is the URI <em>".../WEB-INF/jsp/index.jsp"</em> itself matched by the <em>web.xml</em> filter /* that I set?</p>
  
  <p><strong>EDIT</strong> apparently, no, because this is an exact mapping to <em>index.jsp</em> and hence it bypasses the web.xml url-pattern.  So I still don't get how I can enter that endless loop.</p>
  
  <p>How should I do if I want to intercept everything using a /* url-pattern and yet be able to dispatch/forward/? </p>
  
  <p>I'm not asking about specs/Javadocs here: I'm really confused about the bigger picture and I'd need some explanation as to what could be going on.</p>
  
  <p>Am I not supposed to intercept really everything? </p>
  
  <p>If I can intercept everything, what should I be aware of regarding forwarding/dispatching?</p>