2

Please don't hesitate to edit the question or ask more details if I missed anything.

I know it's bad to use Scriptlets in JSP.

But I am assigned to maintain the existing JAVA project which is build only with only JSP and servlets(No framework).

My task is to implement the load balancing for my applicaiton using Apache HTTP Server.

The application works fine with out load balancing. When I implement the load balancing using the Apache HTTP Server, I am facing the problem with JSP.

I will give a scenario. My JSP has one while loop and it runs the javascript to update the content .

My JSP has,

<%
    String jsPreAppend = "<script language=JavaScript >push('";     
    String jsPostAppend = "')</script> ";   
    String s=null;   
    int i = 0;

       try {
        while (true) {
            System.out.println("count :"+i);
            out.print(jsPreAppend + i + jsPostAppend);        
            out.flush();

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                out.print(jsPreAppend + "InterruptedException: " + e + jsPostAppend);
            }

            i++;

          }
       } catch (Exception e) {
             out.print(jsPreAppend + "Exception: " + e + jsPostAppend);
       }
%> 

My JavaScript has,

 function push(content) {   
         document.getElementById('update').innerHTML = content;
 }

The console output will be,

count :1
count :2
count :3
.
.
.
count : n
count :n+1

But the content will not updated in JSP. I thing the javascript fails in while loop.

But the SysOut() works because the updated content will be printed for every sec in the console .

But the same applicaiton work's fine with out load balancing(only one tomcat).

Hope our stack users will help me.

Human Being
  • 8,269
  • 28
  • 93
  • 136

2 Answers2

3

When your HTML gets rendered, JSP would have already got executed. So what you are trying to do cannot be achieved by that code.

You need to write a Java script method which does some update once in sometime. Check this thread to write the same logic using Javascript

Community
  • 1
  • 1
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • 2
    But the OP said this worked on a single node. He seems to be building js calls `push(i)` from the scriptlet loop. Those calls are probably meant to be executed on page load or similar. Can you elaborate on what this might have to do with load balancing? – Xavi López Aug 26 '13 at 09:53
  • As per me it should not have worked in both cases. We can't control auto execution of some JS function from JSP right? If `push(i)` is done from JSP, then the delay will be invisible and every time the page loads, then it does `push(i)` calls as per the numbers got printed – sanbhat Aug 26 '13 at 09:57
  • As I see it, the calls to `push(i)` won't be _executed_ from the JSP. They'll be printed out in the HTML response, and they'll be executed by the browser when it sees the ` – Xavi López Aug 26 '13 at 10:06
  • @Xavi My requirement is only with IE. How this will works with out **load balancing** ? – Human Being Aug 26 '13 at 10:07
  • @sanbhat Now I see what you meant, upon looking what does the actual code do, and of course those js calls will not be executed server-side. The code seems seriously flawed, and it should eventually throw an exception or never end. – Xavi López Aug 26 '13 at 10:27
  • @Xavi Its a never end loop. The **while loop** gets executed for every **second** . – Human Being Aug 26 '13 at 10:44
0

Take into account that the while(true) loop will be executed server side. At that point, the response document (the HTML) is being built, and it can't be yet interpreted by the client. This loop is only writing javascript calls to a kind of buffer where the response is stored before it is sent to the client.

As an example, what that loop is doing is writing ad-infinitum to the response:

<script language=JavaScript >push('1')')</script>
...
<script language=JavaScript >push('n')')</script>

The fact that every line is being written at every second is irrelevant. You see the traces in the standard output at the correct times because that's what is being executed on the server.

This will make the request get stuck in that infinite loop unless there's an exception of some kind. Even if the loop ended at some point, and the request finished processing, when these statements would get executed by a client, they would be executed sequentially without any delay.

You should move those calls to client side, and schedule its execution with a client-side mechanism such as setTimeout(), like @sanbhat suggested in his answer.

Xavi López
  • 27,550
  • 11
  • 97
  • 161