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!.

5
  • 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 stackoverflow.com/help/how-to-ask Commented Nov 20, 2020 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. Commented Nov 20, 2020 at 13:47
  • I tried a solution posted with python flask here: stackoverflow.com/questions/15041620/… and it did worked the way I wanted. But unsure about how can we get this done with java stack. Commented Nov 20, 2020 at 13:48
  • 1
    Ok so far this seems to be working for me...logicbig.com/tutorials/spring-framework/spring-web-mvc/…. 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 + "<br/>\n").getBytes()); out.flush(); } This doesnt seem to work!!! Commented Nov 20, 2020 at 16:28
  • Updated answer according to your approach. Line break doesn't work because of the content type. See answer below. Commented Nov 20, 2020 at 17:31

1 Answer 1

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);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Martin for this, it worked :).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.