title: Servlet 3.0: Can't send an asynchronous response?
body: <p>I'm having trouble establishing AsyncContexts for users and using them to push notifications to them. On page load I have some jQuery code to send the request:</p>
  
  <pre><code>$.post("TestServlet",{
      action: "registerAsynchronousContext"
          },function(data, textStatus, jqXHR){
              alert("Server received async request"); //Placed here for debugging   
    }, "json");
  </code></pre>
  
  <p>And in "TestServlet" I have this code in the doPost method:</p>
  
  <pre><code>HttpSession userSession = request.getSession();
  String userIDString = userSession.getAttribute("id").toString();
  
  String paramAction = request.getParameter("action");
  
  if(paramAction.equals("registerAsynchronousContext"))
  {              
      AsyncContext userAsyncContext = request.startAsync();
  
      HashMap&lt;String, AsyncContext&gt; userAsynchronousContextHashMap = (HashMap&lt;String, AsyncContext&gt;)getServletContext().getAttribute("userAsynchronousContextHashMap");
      userAsynchronousContextHashMap.put(userIDString, userAsyncContext);
      getServletContext().setAttribute("userAsynchronousContextHashMap", userAsynchronousContextHashMap);
  
      System.out.println("Put asynchronous request in global map");
  }
  
      //userAsynchronousContextHashMap is created by a ContextListener on the start of the web-app
  </code></pre>
  
  <p>However, according to Opera Dragonfly (a debugging tool like Firebug), it appears that the server sends an HTTP 500 response about 30000ms after the request is sent.</p>
  
  <p>Any responses created with userAsyncContext.getResponse().getWriter().print(SOME_JSON) and sent before the HTTP 500 response is not received by the browser, and I don't know why. Using the regular response object to send a response (response.print(SOME_JSON)) is received by the browser ONLY if all the code in the "if" statement dealing with AsyncContext is not present.</p>
  
  <p>Can someone help me out? I have a feeling this is due to my misunderstanding of how the asynchronous API works. I thought that I would be able to store these AsyncContexts in a global map, then retrieve them and use their response objects to push things to the clients. However, it doesn't seem as if the AsyncContexts can write back to the clients.</p>
  
  <p>Any help would be appreaciated.</p>