JSP processing progress indication

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dzenanz
    New Member
    • Feb 2008
    • 45

    JSP processing progress indication

    I have JSP web-form which uploads a file. In java code, I have a statement like this:

    Code:
    for all lines in txt file uploaded
      call DB stored procedure
    As this web interface should be used over a network, and not internet, file upload ends pretty quickly, but DB loop lasts for minutes (if file is longer).

    How can I give indication of loop's progress to the user? Percentage or progress bar would be ideal, printing a dot for each line processed would be acceptable.

    Any combination of technologies are in play: javascript, multithreading, (I allready use iframe on the form page), etc...

    Also, when explaining, you should have in mind I am new to java platform (I have more experience with ASP.net).
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by dzenanz
    I have JSP web-form which uploads a file. In java code, I have a statement like this:

    Code:
    for all lines in txt file uploaded
      call DB stored procedure
    As this web interface should be used over a network, and not internet, file upload ends pretty quickly, but DB loop lasts for minutes (if file is longer).

    How can I give indication of loop's progress to the user? Percentage or progress bar would be ideal, printing a dot for each line processed would be acceptable.

    Any combination of technologies are in play: javascript, multithreading, (I allready use iframe on the form page), etc...

    Also, when explaining, you should have in mind I am new to java platform (I have more experience with ASP.net).
    Have you tried something like out.print as the last statement of you for loop?

    Comment

    • dzenanz
      New Member
      • Feb 2008
      • 45

      #3
      Originally posted by r035198x
      Have you tried something like out.print as the last statement of you for loop?
      No, but I assume that I would also have to flush the stream, and I am not sure that browser would display data as soon as it arrives.

      I wanted to check for ideas on this forum first.

      If println works, what do you think, would something like this work:

      Code:
      output header:
      ...
      <input type="text" name="perc" id="perc" value="0%" />
      <script language="javascript">
      label=document.getElementById("perc");
      and then in loop:
      Code:
      {
      ...
      out.println("label.value='"+i+"%';");
      out.flush();
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by dzenanz
        No, but I assume that I would also have to flush the stream, and I am not sure that browser would display data as soon as it arrives.

        I wanted to check for ideas on this forum first.

        If println works, what do you think, would something like this work:

        Code:
        output header:
        ...
        <input type="text" name="perc" id="perc" value="0%" />
        <script language="javascript">
        label=document.getElementById("perc");
        and then in loop:
        Code:
        {
        ...
        out.println("label.value='"+i+"%';");
        out.flush();
        }
        Perhaps a bit more info on your problem ...
        That for-loop is executed on the server and is run once before the page is rendered right? If you want some interactive behaviour then you might have to try some AJAX stuff.

        Comment

        • dzenanz
          New Member
          • Feb 2008
          • 45

          #5
          What AJAX frameworks for Java are there? I mean, for this concrete problem, is it easier to integrate some asynchronous calls or add iframe to resulting page that will be refreshed by javascript (by polling status on the request every few seconds or so)?

          Comment

          • dzenanz
            New Member
            • Feb 2008
            • 45

            #6
            Solution

            I solved it like this:
            • Add text element in a div
            • Hide div in body onload
            • For each progress update call update_progress function ("up") wrapped inside <script> tags. If all calls are made inside one script tag, they are executed when browser encounters closing script tag, which is in my case - when the processing ends

            example of output:
            [HTML]<html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>page title</title>
            </head>
            <body onload="documen t.getElementByI d('progress').s tyle.display='n one'">

            <div id="progress">< br />Progress: <input type="text" id="progressTex t" value="0%" readonly="reado nly">
            </div>

            <script type="text/javascript">
            var pp = document.getEle mentById("progr essText");
            function up(p)
            {
            pp.value=p.toSt ring(10)+'%';
            }
            </script>

            <script type="text/javascript">
            up(1);
            </script>
            <script type="text/javascript">
            up(2);
            </script>
            <script type="text/javascript">
            up(3);
            </script>
            ...
            <script type="text/javascript">
            up(97);
            </script>
            <script type="text/javascript">
            up(98);
            </script>
            <script type="text/javascript">
            up(99);
            </script>
            <script type="text/javascript">
            up(100);
            </script>
            <br />
            <br /><h2>Success</h2><br />
            -rest of page-
            </body>
            </html>[/HTML]of course[HTML]<script type="text/javascript">
            up(2);
            </script>[/HTML]are generated in a for loop with this:
            Code:
            if (percentage.intValue() > oldPrecentage) {
            	oldPrecentage = percentage.intValue();
            	out.printf(progressString, new Object[]{percentage});
            	out.flush();
            }
            with
            Code:
            progressString="<script type=\"text/javascript\">\nup(%d);\n</script>\n";
            Dženan
            Last edited by dzenanz; Apr 10 '08, 12:25 PM. Reason: add/remove a few empty lines in code

            Comment

            Working...