Reputation: 3903
I have a webappliaction(developed in Jsp & Severlet) that execute the sh script and displays the output in browser only after executing the script but i want to print the output like how its executing in teriminal (one by one for eg ping command).So that user will have the experience like working in unix terminal. My script will run almost one minute(Script to start stop my WAS servers) so the user should not wait till one minute to see the final output . They should see the script started output once they start the process .please find my sample code below.
pb = new ProcessBuilder("/bin/sh",script);
pb.directory(new File(filePath));
p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null){
System.out.println(line);
out.println(line);
}
p.waitFor();
System.out.println ("exit: " + p.exitValue());
out.println("exit: " + p.exitValue());
p.destroy();
out.println("Script Executed");
Please anyone guide me.
Upvotes: 2
Views: 1276
Reputation: 3903
Finally i got a solution for my problem, I just added out.flush()
after out.println(line);
So its flushing output to browser each time in while loop and its looks like unix terminal . Below code did the magic.
pb = new ProcessBuilder("/bin/sh",script);
pb.directory(new File(filePath));
p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null){
System.out.println(line);
out.println(line);
out.flush();
}
p.waitFor();
System.out.println ("exit: " + p.exitValue());
out.println("exit: " + p.exitValue());
p.destroy();
out.println("Script Executed");
Upvotes: 2
Reputation: 67457
Your problem is invisible here in the code example because it works just fine locally (e.g. for System.out
). So logic implies that the problem must be somewhere in networking between client and server. Neither do you show how out
is created (I guess it is some kind of socket-connected PrintStream
) nor how the JSP reads it. What you want is some AJAX approach (XmlHttpRequest or similar) and probably you do not use it, but some "naïve", old-fashioned way. I am not a web developer, but your favourite search engine or some other people here might be able to help you with that part.
Upvotes: 1