Monday, 26 August 2013

While loop in JSP to execute javascript not working?

While loop in JSP to execute javascript not working?

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.

No comments:

Post a Comment