1

I have a scenario wherein we need to trigger a command line process from the web page(on click of a button). Then, I need to display continuously like a live feeds of all command line messages onto the browser maybe in a div...any thoughts how can we achieve this?.

Also, I'm open to get this resolved with any java/javascript web framework....but preferably spring boot.

Thank you!.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • The answer to "Can we display the terminal output to html with spring boot" is: yes. But there as many tasks to get there. What did you try already? Where are you stuck at the moment? See https://stackoverflow.com/help/how-to-ask – martinspielmann Nov 20 '20 at 13:45
  • TBH, I'm unable to display anything or rather do not have any idea of how can we display the terminal output back as continuous response. – Ravi Salunkhe Nov 20 '20 at 13:47
  • I tried a solution posted with python flask here: https://stackoverflow.com/questions/15041620/how-to-continuously-display-python-output-in-a-webpage and it did worked the way I wanted. But unsure about how can we get this done with java stack. – Ravi Salunkhe Nov 20 '20 at 13:48
  • 1
    Ok so far this seems to be working for me...https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/streaming-response-body.html. But, how can I format it so that the output renders in a new line? BufferedReader br1 = new BufferedReader(new InputStreamReader(pr.getErrorStream())); String line1 = null; while ((line1 = br1.readLine()) != null) { // System.out.println(line); out.write((Integer.toString(i) + line + "
    \n").getBytes()); out.flush(); } This doesnt seem to work!!!
    – Ravi Salunkhe Nov 20 '20 at 16:28
  • Updated answer according to your approach. Line break doesn't work because of the content type. See answer below. – martinspielmann Nov 20 '20 at 17:31

1 Answers1

0

With Spring Boot, you could give spring-boot-starter-websocket a try. A working tutorial projecte can be downloaded here: https://spring.io/guides/gs/messaging-stomp-websocket/

A simpler approach to websockets (but also less flexible) is using StreamingResponseBody as you already mentioned. The reason why the <br /> is not working as stated in your last comment is, that the default content type returned will be text/plain. Update your controller like shown below to set the content type to HTML, so the browser will render it accordingly.

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

@Controller
public class StreamingController {

  @RequestMapping(value = "/")
  public ResponseEntity<StreamingResponseBody> handleRequest2() {
    StreamingResponseBody stream = out -> {
      for (int i = 0; i < 1000; i++) {
        out.write((Integer.toString(i) + " - <br />").getBytes());
        out.flush();
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    };
    return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(stream);
  }
}
martinspielmann
  • 536
  • 6
  • 19