1

I am trying to create a Java (not javascript, but I could include it for sure) webserver which will be able to print a live console from another Java program to a webpage. I know how to make the Java webserver, and I know how to access the Input and Output streams for the other running Java program, but I do not know how to send console output and get console input from the webpage the person is viewing.

I could use html and have the page reload every half second, but then no one could type anything to send to the console.

Someone recommended I use ASP, but I have no idea where to even start.

To recap, I want to send and show live data to a webpage and receive live user input back to the webserver.

For reference, I am using the following for the output I want to send to the webpage:

ProcessBuilder pb = new ProcessBuilder("java", "-jar", "my.jar");
pb.directory(new File("C:/Users/Michael Forseth/Desktop/Server Stuff/Test Moltres/TEST SERVER"));
final Process p = pb.start();

InputStream in = p.getInputStream();
InputStreamReader ins = new InputStreamReader(in);
BufferedReader br = new BufferedReader(ins);

String line;
while((line = br.readLine()) != null){
    //Send "line" to webpage for user to see
    System.out.println("CONSOLE: " + line);
}
Forseth11
  • 1,418
  • 1
  • 12
  • 21

1 Answers1

1

You are describing how a modern website works. A java webserver listens for http requests and returns data (typically json or html format). To avoid reloading the page in the browser (the client) so the user can not type, you can just reload a part of the page. This is done with ajax, and the easiest way to get going is to use javascript and a library called jQuery. If you don't want to learn javascriot for this, there is a javascript library called intercoolerjs, which allows you to enable ajax with plain html attributes.

Edit: if you need to have the server update the page without the user doing anything, you have to read about websockets. Hope this sends you in the right direction.

Mats Faugli
  • 161
  • 8
  • I guess I will be looking up websockets, as I need it to update without the user doing anything. Would it be possible to make some kind of js timer to request the ajax to update every second or so? – Forseth11 May 22 '17 at 23:38
  • Websockets does not close the connection after one request as with the http protocol, thus you have to make adjustement to your web server. You might be just fine with a simple js timer, check out [this answer](https://stackoverflow.com/questions/6835835/jquery-simple-polling-example) which provides a good example for this. If you want to look into intercoolerjs, have a look at [this example](http://intercoolerjs.org/attributes/ic-poll-repeats.html) – Mats Faugli May 23 '17 at 14:33
  • I got it to work via WebSockets. I found this example here: http://restlessdev.com/blog/websockets-in-java and I was able to make changes from it and ended up with what looks like this now, and it works to interact with the commands I made in Java: [finished picture](https://i.gyazo.com/37ae5cf4ac6bc014c22b2fab03b52f0d.png) – Forseth11 May 25 '17 at 05:46
  • @Forseth11, Thanks for your solution. I've an exactly same requirement and need to have a same implementation. If you could share your entire code snippets or source-repo for both client-side and server-side, it would be add a great value for the reference. Thanks in advance. – GPPanda Jun 02 '21 at 05:51