title: Handling a Long Running jsp request on the server using Ajax and threads
body: <p>I am trying to implement a solution for a long running process on the server where it is taking about 10 min to process a pdf generation request. The browser bored/timesout at the 5 mins. I was thinking to deal with this using a Ajax and threads. I am using regular javascript for ajax. But I am stuck with it.</p>
  
  <p>I have reached till the point where it sends the request to the servlet and the servlet starts the thread.Please see the below code</p>
  
  <pre><code>public class HelloServlet extends javax.servlet.http.HttpServlet 
       implements javax.servlet.Servlet {
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) 
          throws ServletException, IOException {      
      }   
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) 
          throws ServletException, IOException {
          System.out.println("POST request!!");
          LongProcess longProcess = new LongProcess();
          longProcess.setDaemon(true);
          longProcess.start();
          request.getSession().setAttribute("longProcess", longProcess);
          request.getRequestDispatcher("index.jsp").forward(request, response);
      }   
   }
  
      class LongProcess extends Thread {
  
          public void run() {
              System.out.println("Thread Started!!");
              while (progress &lt; 10) {             
                  try { sleep(2000); } catch (InterruptedException ignore) {}
                  progress++;
              }
          }           
      }
  </code></pre>
  
  <p>Here is my AJax call </p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;&lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;My Title&lt;/title&gt;
  &lt;script language="JavaScript" &gt;
  function getXMLObject()  //XML OBJECT
      {
         var xmlHttp = false;
           xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
         return xmlHttp;  // Mandatory Statement returning the ajax object created
      }
  
      var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object
  
      function ajaxFunction() {
          xmlhttp.open("GET","HelloServlet" ,true);
          xmlhttp.onreadystatechange  = handleServerResponse;
          xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
          xmlhttp.send(null);
        }
  
      function handleServerResponse() {
         if (xmlhttp.readyState == 4) {
           if(xmlhttp.status == 200) {           
             document.forms[0].myDiv.value = xmlhttp.responseText;
             setTimeout(ajaxFunction(), 2000);
           }
           else {
              alert("Error during AJAX call. Please try again");
           }
         }
      }
      function openPDF() {    
             document.forms[0].method = "POST";
          document.forms[0].action = "HelloServlet";
          document.forms[0].submit();     
      }
      function stopAjax(){
           clearInterval(intervalID);
       }
  &lt;/script&gt;
  &lt;/head&gt; 
  &lt;body&gt;&lt;form name="myForm"&gt;
  &lt;table&gt;&lt;tr&gt;&lt;td&gt;
  &lt;INPUT TYPE="BUTTON" NAME="Download" VALUE="Download Queue ( PDF )" onclick="openPDF();"&gt;
  &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;
  Current status: &lt;div id="myDiv"&gt;&lt;/div&gt;%
  &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  &lt;/form&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>But I dont know how to proceed further like how will the thread communicate the browser that the process has complete and how should the ajax call me made and check the status of the request.</p>
  
  <p>Please let me know if I am missing some pieces. Any suggestion if helpful.</p>