Reputation: 1438
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);
}
Upvotes: 1
Views: 735
Reputation: 169
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.
Upvotes: 1