title: Get AsyncContext from HttpServletRequest
body: <p>I'm using Spring's <code>OncePerRequestFilter</code> overriding <code>shouldNotFilterAsyncDispatch</code> method to return false. This way it can handle asynchronous requests. In the filter I'm trying to do the following:</p>
  
  <pre><code>if (isAsyncDispatch(request)) {
      request.getAsyncContext().addListener(new AsyncListener() {
          @Override
          public void onComplete(AsyncEvent event) throws IOException {
              System.out.println("onComplete");
          }
  
          @Override
          public void onTimeout(AsyncEvent event) throws IOException {
  
          }
  
          @Override
          public void onError(AsyncEvent event) throws IOException {
              System.out.println("onError");
          }
  
          @Override
          public void onStartAsync(AsyncEvent event) throws IOException {
  
          }
      });
  }
  </code></pre>
  
  <p>So <code>isAsyncDispatch</code> returns true as expected. But when I try <code>getAsyncContext</code> it fails with the following exception:</p>
  
  <pre><code>IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
  </code></pre>
  
  <p>Indeed, <code>request.isAsyncStarted()</code> returns false, but <code>request.isAsyncSupported()</code> is true and <code>request.getDispatcherType()</code> is <code>ASYNC</code>. </p>
  
  <p>I don't get: is it async or not? Maybe I'm using the API in a wrong way? How do I add an <code>AsyncListener</code>? Maybe it is because I'm using Tomcat?</p>
  
  <p>Thank you in advance!</p>