Executing Unix Commands in a JSP and Displaying The Output in the Browser
Here is a JSP that executes a series of Unix commands, and displays the results in the Browser. It opens two InputStreams, one for regular output, and the other for error output. A regular output line is prefaced by "Out", and the error output is prefaced by "Error": If you want the output to go to the out.log, say System.out.println() instead of out.println(), and include an out.println("commands executed, output send to out.log);
<%@ page import="java.io.*" %> <pre> <% try { Runtime r = Runtime.getRuntime(); String msg = "", emsg = ""; String cmd = "/bin/sh -c date;pwd;badcmd;ls"; Process p = r.exec(cmd); p.waitFor(); BufferedReader inOut = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader inErr = new BufferedReader(new InputStreamReader (p.getErrorStream())); while ((msg = inOut.readLine()) != null) { out.println("Out = " + msg); } while ((emsg = inErr.readLine()) != null) { out.println("Error = "+ emsg); } p.destroy(); } catch(Exception e) { out.println(e.toString()); } %> </pre>