title: Could not understand how AsyncContext.dispatch() works when AsyncContext.startAsync() and AsyncContext.startAsync(req, res) are used
body: <p>I'm simply dispatching HttpServletRequest and HttpServletResponse objects from Servlet1 to Servlet2 via req.getRequestDispatcher("/servlet2").forward(req, res).</p>
  
  <p>In Servlet2, I start asynchronous cycle using req.startAsync(req, res), complete it and call dispatch() at the end.</p>
  
  <p>Now, according to oracle docs, "<strong><em>If the asynchronous cycle was started with ServletRequest#startAsync(ServletRequest, ServletResponse), and the request passed is an instance of HttpServletRequest, then the dispatch is to the URI returned by HttpServletRequest.getRequestURI(). Otherwise, the dispatch is to the URI of the request when it was last dispatched by the container.</em></strong>
  ".</p>
  
  <p>But when I call dispatch(), the request is dispatched to Servlet1.</p>
  
  <p>This is Servlet1</p>
  
  <pre><code>@WebServlet(asyncSupported=true, urlPatterns={"/servlet1"})
  public class Servlet1 extends HttpServlet
  {
      public void service(HttpServletRequest req, HttpServletResponse res) 
  throws IOException, ServletException
      {
          PrintWriter out = res.getWriter();
          res.setContentType("text/html");
          out.println("&lt;html&gt;&lt;center&gt;&lt;body&gt;");
          out.println("Setting attributes in requeset object&lt;br&gt;");
          req.setAttribute("name", "Abhishek");
          out.println("forwarding request&lt;br&gt;");
          req.getRequestDispatcher("/servlet2").forward(req, res); // dispatch 
          req, res to Servlet2
          out.println("back in Servlet1&lt;br&gt;");
          out.println("getRequestURI:"+req.getRequestURI()+"&lt;br&gt;");
          out.println("servlet1 is getting attribute that was set by 
          servlet2&lt;br&gt;");
          if(req == null)
              out.println("req is null&lt;br&gt;");
          else
              out.println(((Integer)req.getAttribute("k")).intValue()+"&lt;br&gt;");
      }
  }
  </code></pre>
  
  <p>This is Servlet2</p>
  
  <pre><code>@WebServlet(asyncSupported=true, urlPatterns={"/servlet2"})
  public class Servlet2 extends HttpServlet
  {
      public void doGet(HttpServletRequest req, HttpServletResponse res) 
  throws IOException
      {
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
          out.println("&lt;html&gt;&lt;center&gt;&lt;body&gt;");
          out.println("getting attributes from request object&lt;br&gt;");
          out.println((String)req.getAttribute("name")+"&lt;br&gt;");
          out.println("getRequestURI:"+req.getRequestURI()+"&lt;br&gt;");
          out.println("Starting asynchronous cycle&lt;br&gt;");
          AsyncContext context = req.startAsync((ServletRequest)req, 
          (ServletResponse)res);
          out.println("Is Async Supported? "+ req.isAsyncSupported()+"&lt;br&gt;");
          out.println("Is Async Started? "+ req.isAsyncStarted()+"&lt;br&gt;");
          out.println("Servlet 2 is now setting attributes&lt;br&gt;");
          req.setAttribute("k", 9);
          context.complete(); 
          context.dispatch(); 
          out.println("&lt;/body&gt;&lt;/center&gt;&lt;/html&gt;");
      }
  }
  </code></pre>
  
  <p>I'm expecting it to be dispatched to Servlet2 itself since req.getRequestURI() returns Servlet2 URI. Why is it getting dispatched to Servlet1? Please help.</p>