Launch an external program, capture its output and display it in a JSP

From CodeCodex

Implementations[edit]

Java[edit]

In this HowTo, the Windows utility NET is executed to get the members of a given group. The JSP captures the output and to display it as Web page.
<H1>Members of the <%=request.getParameter("group")%> group</H1>
<%

String cmdline = "net group " + request.getParameter("group") + " /domain";

out.println("<pre>");
    try {
     String line;
     Process p = Runtime.getRuntime().exec(cmdline);
     BufferedReader input = 
       new BufferedReader
         (new InputStreamReader(p.getInputStream()));
     while ((line = input.readLine()) != null) {
       out.println(line);
       }
     input.close();
     } 
    catch (Exception err) {
     err.printStackTrace();
     }
out.println("
");

%> </pre> As always, special care must be taken if there is a risk that the called program can write something into stderr (then the process will hang forever).