0

I am working on jsp, What I am trying to implement is quite simple.

  • do some time-consuming work with Java
  • while processing, show users the spinner

So I have implemented like below

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page isThreadSafe="true" %>
<%

    boolean isFinish = false;
    Thread executeThread = new Thread(new Runnable() {
        @Override
        public void run() {
            for(int i = 0; i < 10; i++){
                try{
                    System.out.println(i + " Thread ");
                    Thread.sleep(1000);
                }catch(Exception ex){
                    System.err.println(ex);
                }
            }
        }
    });

    executeThread.run();
%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8'>
        <title>Loading</title>
    </head>
    <body>
        <div class="popup" style="text-align: center;">
            <div class="l_img">
                <img src="resources/image/spinner/loading_gif.gif" alt="LOADING...">
            </div>
            <div class="b_txt">Processing</div>
            <div class="s_txt">Please, wait..</div>
        </div>
    </body>
    <%
        if(isFinish){
    %>
        <script>
            alert('done');
        </script>
    <%
        }
    %>
</html>

However, page shows up after Thread has terminated. Could I reverse the order? What I wanted in order

  • show html page(spinner gif)
  • execute Java logics
  • catch the return of Java execution
  • if return is true, then alert with javascript

Is it possible? Thanks for your answers

Juneyoung Oh
  • 7,318
  • 16
  • 73
  • 121

2 Answers2

1

You can't do a server-side operation change dinamically the render the page.

the page will be rendered always where if(isFinish) has been executed.

the server will send the complete html page to the user only when all the java has been executed and in one block! you need to implement it on the front-end, for example you send a starting basic page of loading and you call the long term process with a ajax call or something similar.

Emanuele Parisio
  • 1,192
  • 8
  • 27
  • Thanks for your answer. Since I have implemented with `Thread`, I thought it should showed up whether Thread is running or not. However, i counts 10 and show page in order. ( I got your point -front-end handling - but it can not be like that in my case) Do I have any other options? – Juneyoung Oh Jun 17 '16 at 10:51
0
<!DOCTYPE html>
<html>
<body onload="alert('done')">
    Put your spinner here.
    Some text here is necessary here for the browser to start displaying. 
    Otherwise it will wait until it receives enough of a response to begin displaying anything. 
    I am not sure how much is necessary. This works for me.
<%
    out.print("<br/>Starting processing now. <br/>");
    out.flush();
    Thread.sleep(2000);
    out.print("<br/>Processing..... <br/>");
    out.flush();
    for(int x = 1; x < 4; x++){
        Thread.sleep(2000); 
        out.print("Result " + x + ".<br/>"); 
        out.flush();
    }
%>
</body>
</html>
rickz
  • 4,324
  • 2
  • 19
  • 30
  • Thanks for answer. It works. However, basically can not control server side script order. So I picked other answer. Thanks though – Juneyoung Oh Sep 11 '16 at 07:58