title: Why does my Servlet Filter run twice?
body: <p>I have a simple Servlet filter:</p>
  
  <pre><code>package net.twentyonesolutions;
  
  import javax.servlet.*;
  import javax.servlet.http.HttpServletRequest;
  import java.io.IOException;
  import java.util.Date;
  
  public class TestFilter implements Filter {
  
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  
          System.out.println(TestFilter.class.getSimpleName()       // line 12
                  + " &gt; "
                  + ((HttpServletRequest)servletRequest).getRequestURL()
                  + ": IP "+ servletRequest.getRemoteAddr()
                  + "; at "
                  + new Date().toString());
  
          // Pass request back down the filter chain
          filterChain.doFilter(servletRequest, servletResponse);    // line 20
      }
  
      public void init(FilterConfig filterConfig) throws ServletException {}
  
      public void destroy() {}
  }
  </code></pre>
  
  <p>I added it to web.xml like so:</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;TestFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;net.twentyonesolutions.TestFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;TestFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>I am running it in debug mode in IntelliJ IDEA with an embedded Jetty.  I expect the output from line 12 to be written once, but instead it is written twice, and both times are before the Servlet is called.</p>
  
  <p>Why is it called twice?  Is it supposed to be called twice?  If so, how can I tell the first time from the second time, because I want my code to only run once.</p>
  
  <p>I found a couple of old questions about this issue, but none had a real solution or explanation.</p>
  
  <p>You can see the diff of the stack trace between the 1st call and the 2nd call in the screenshot below:</p>
  
  <p><a href="https://i.stack.imgur.com/mv65o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mv65o.png" alt="enter image description here"></a></p>
  
  <h2>UPDATE</h2>
  
  <p>When I deploy the filter to regular (not-embedded) Jetty (same version as the embedded one, <code>9.2.18.v20160721</code>) or Tomcat -- <code>doFilter()</code> is only executed once per request, as expected, so the issue is either with the debugging in IntelliJ IDEA or with the embedded Jetty.</p>
  
  <h2>Update embed code:</h2>
  
  <pre><code>    WebAppContext webapp = new WebAppContext();
      webapp.setContextPath("/");
      webapp.setResourceBase(webRoot);
      webapp.setDefaultsDescriptor(webRoot + "/WEB-INF/web.xml");     
  
      Server server = new Server();
  
      ServerConnector http = new ServerConnector(server);
      http.setPort(port);             
      if (!host.equals(""))
          http.setHost(host);
  
      server.setConnectors(new ServerConnector[] { http });
      server.setHandler(webapp);      
      server.start();
      server.join();
  </code></pre>
  
  <h2>Update filterChain.toString()</h2>
  
  <p>I added a print of filterChain.toString() just before the call <code>filterChain.doFilter()</code> and it shows the difference clearly.  </p>
  
  <p>The first object in the <code>filterChain</code> (before the first call to <code>doFilter()</code>), is my filter.  See the two printed lines:</p>
  
  <pre><code>TestFilter &gt; fdvnqp1k0ut37fenee569vshqf http://localhost:8080/index.cfm: IP 0:0:0:0:0:0:0:1; at Tue Sep 06 09:59:09 PDT 2016
      ... filterChain: TestFilter-&gt;CFMLServlet@cdad6a3==lucee.debug.loader.servlet.CFMLServlet,1,true
  TestFilter &gt; fdvnqp1k0ut37fenee569vshqf http://localhost:8080/index.cfm: IP 0:0:0:0:0:0:0:1; at Tue Sep 06 09:59:09 PDT 2016
      ... filterChain: CFMLServlet@cdad6a3==lucee.debug.loader.servlet.CFMLServlet,1,true
  </code></pre>
  
  <p>I'm not sure about the spec, but I expect my <code>TestFilter</code> to be removed from the chain before <code>TestFilter.doFilter()</code> is called the first time, no?</p>