title: Long polling in JQuery + JAVA?
body: <p>I need to <strong>hold</strong> my request in the <strong>server</strong> until new data comes.I am using the <strong>Tomcat 6</strong>  as my web server . So  this is my <strong>JQuery</strong> code,</p>
  
  <pre><code>function sendMessage() {
  
      var message = $("#message").val();
      $.ajax({
          type: "POST",
          cache: false,
          url: "sendMessage.html",
          data: "message=" + message,
          dataType: "html",
  
          success: function(response) {
  
          },
          error: function(e) {
              //alert('Error: ' + e);
          },
      });
  }
  
  
  function startLongPolling(){
  
      $.ajax({
          type: "POST",
          cache: false,
          url: "LongPoll.html",
          dataType: "html",
          success: function(response) {   
              if(response != null &amp;&amp; response !="" &amp;&amp; response !="null")
                  $("#sucess").html(response);
          },
          error: function(e) {
              //alert('Error: ' + e);
          },
  
          complete: function(e) {
              startLongPolling();
          },
      });
  }
  </code></pre>
  
  <p>My <strong>java</strong> code will be ,</p>
  
  <pre><code>@RequestMapping(value = "LongPoll.html", method=RequestMethod.POST )
  public @ResponseBody String longLongPolling(HttpSession session) {
  
      String sessionId = session.getId().toString();
      AgentState agentState = ApplicaionManager.agentDetail.get(sessionId);
      String message = null;
  
      if(ApplicaionManager.agentDetail.containsKey(sessionId)){
  
          while(true){
  
              if(agentState.isStateChange() == true){
                  message = agentState.getMessage();
                  if(message != null)
                      agentState.setStateChange(false);
                  System.out.println("Break for session "+sessionId+" due to Agent State changed");
                  break;
              }
  
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
      System.out.println("While exited for session"+sessionId);
      return message;
  }
  </code></pre>
  
  <p>But there is a <code>continous request sent to server</code> for every <strong>11 seconds</strong> . I don't know how it is possible. I have checked this with <strong>chrome developer tools</strong>.</p>
  
  <p><img src="https://i.stack.imgur.com/pEvGw.png" alt="enter image description here"></p>
  
  <p>Hope our stack users will help me.</p>