title: servlet displaying console values to browser
body: <p>I have a servlet program (Got it from a friend), it is executing few command prompt commands and returing the output in console now I would like to see all that multi line console output lines in my browser, how to I do this?</p>
  
  <pre><code>public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
  {
        String[] command =
              {
                  "zsh",
              };
  
              Process p = Runtime.getRuntime().exec(command);
              new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
              new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
              PrintWriter stdin = new PrintWriter(p.getOutputStream());
              stdin.println("source ./taxenv/bin/activate");
              stdin.println("python runner.py");
              // write any other commands you want here
              stdin.close();
              int returnCode = 0;
              try {
                  returnCode = p.waitFor();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } System.out.println("Return code = " + returnCode);
  
  
  }
  
  class SyncPipe implements Runnable
  {
  public SyncPipe(InputStream istrm, OutputStream ostrm) {
        istrm_ = istrm;
        ostrm_ = ostrm;
    }
    public void run() {
        try
        {
            final byte[] buffer = new byte[1024];
            for (int length = 0; (length = istrm_.read(buffer)) != -1; )
            {
                ostrm_.write(buffer, 0, length);
  
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    private final OutputStream ostrm_;
    private final InputStream istrm_;
  }
  </code></pre>