title: How do you fix infinite redirects to a Servlet?
body: <p>I have a Servlet mapped as follows:</p>
  
  <pre><code>@WebServlet("/test/*")
  </code></pre>
  
  <p>So that everything accessing <code>localhost:8080/test/1</code> or <code>localhost:8080/test/99</code> is handled in the same Servlet and forward them to <code>localhost:8080/test/1/test.html</code></p>
  
  <p>The problem is that - my forwarding is falling under the same wildcard rule which is <code>test/*</code> - and is causing an infinite loop.</p>
  
  <p>I've tried to fix this with no luck. I've been thinking of adding some kind of counter to keep track of how many times the Servlet has been called, but that would only limit me and create another problem if I want to reuse the Servlet properly.</p>
  
  <p>All help is appreciated since I've been stuck with this the whole day. Thank you thank you thank you!</p>
  
  <pre><code>@WebServlet("/test/*")
  public class WildcardTest extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              System.out.println(request.getRequestURL());
              RequestDispatcher view = request.getRequestDispatcher("test.html");
              view.forward(request, response);    
      }
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doGet(request, response);
      }
  }
  </code></pre>
  
  <p>Resolution:
  I used the WEB-INF directory to store your html views. You can forward there without hitting any servlet mappings. I thought this would solve it but it still gives me the same problem.</p>
  
  <pre><code> RequestDispatcher view = request.getRequestDispatcher("WEB-INF/views/login.html");
  </code></pre>
  
  <p>Output when requesting <a href="http://localhost:8080/test/test" rel="nofollow">http://localhost:8080/test/test</a>:</p>
  
  <pre><code>2016-05-15T21:12:37.798+0200|Info: http://localhost:8080/test/test
  2016-05-15T21:12:37.798+0200|Info: http://localhost:8080/test/WEB-INF/views/login.html
  2016-05-15T21:12:37.798+0200|Info: http://localhost:8080/test/WEB-INF/views/WEB-INF/views/login.html
  2016-05-15T21:12:37.798+0200|Info: http://localhost:8080/test/WEB-INF/views/WEB-INF/views/WEB-INF/views/login.html
  2016-05-15T21:12:37.798+0200|Info: http://localhost:8080/test/WEB-INF/views/WEB-INF/views/WEB-INF/views/WEB-INF/views/login.html
  </code></pre>