Link to home
Start Free TrialLog in
Avatar of AirFax
AirFax

asked on

Progress-Bar using JSP/Servlets

Hi, I want to reproduce this using a servlet:
http://www.atgconsulting.com/progressbar.asp

What it does in short is rendering an HTML page sending only a part of it every few seconds. I tried to have the servlet wait for a period of time. I tried to use a timer. And I tried to big loops... Nothing seems to do it for me... Many points for you if you come up with a working example! :o)
Avatar of heyhey_
heyhey_

this is client side HTML/DHTML/JavaScipt effect. all you need to do is to save the .html / JavaScript sources from that site and put it on your server
Avatar of girionis
 You could also do a View Source and copy paste the code.
public final class ProgressBar extends Thread {
  private Writer writer;
  public final void setWriter(Writer writer) {
    this.writer = writer;
  }
  public final void run() {
    int progressLevel = 1;
    while(progressLevel < 100) {
      progressLevel += 1;
      writer.print("<script>ProgressBar.length=\"" + progressLevel + "\"</script>\n");");
    }
    writer.print("</html>");
  }
}

In your servlet, pass your writer to your progressBar.
Avatar of AirFax

ASKER

heyhey, this is a server-side trick. I want to provide feedback to the user on something going on in the data-tier. I need the response to last as long as this process.
Avatar of AirFax

ASKER

jerelw, you are almost there. I tried that already... The problem with this is the client will only see the end result because of response buffering.
out.flush();

You can see an example of this at www.findchips.com
Print and flush everything you want to display until your progress bar begins.

Then print and flush until your progress bar ends.

What you would do is print a <script> that increases the size of the image representing your progress bar everytime.

add this to your run() method:

checkProgress() {
  while(notFinished) {
    //do stuff
    notFinished = doDatabaseStuff();
    sleep(5000);
    totalTime = System.currentTimeMillis() - startTime;
    if(totalTime > DEFAULT_LIMIT) {
      out.println("<script>displayTimeout();</script>");
      break;
    }
  }
}

I still don't know how you're going to 'know' what ti indicate in your progress bar if you're simply doing a select.  

If you're doing multiple inserts and updates, I guess you could increment a percentage after each transaction and set to 100% after the commit.

ASKER CERTIFIED SOLUTION
Avatar of jerelw
jerelw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial