Using JSP to report the progress of a Process that Runs Slowly

It sometimes happens that a response to a Web request takes a while. One way to handle this is to create a subtask that progressively reports on its progress.

Here is the JSP page.

<%
     MyThread mt = new MyThread(response, out);
     mt.start();
%>

Here is a class that does the work, and responds over time on its progress. Make sure that servlet.jar is in the Classpath.

import javax.servlet.jsp.*;
import javax.servlet.http.*;
public class MyThread extends Thread {
HttpServletResponse response = null; JspWriter out = null; int seconds = 0;
  public MyThread (HttpServletResponse response, JspWriter out){
this.response=response;
this.out=out;
for (int i=0;i<5;i++){
doTell();
} }


public void doTell(){
try {
Thread.sleep(3000);
seconds=seconds + 3;
out.println("Waiting " + seconds + " seconds <BR>");
out.flush();

}catch (Exception e){ out.println("Exception in MyThread " + e); } }
}