I have an application, currently can run as a Gui Application or command line only. I want to add support so that its runs a server, and can be controlled via a different computer via web browser.
So it will run continously on port, and I want user to connect to that port in browser and see a summary page, then be able to initiate requests via http get requests.
Its need to work with OpenJdk not just Oracle Java so I guess that stops use of com.sun.httpserver.
This is an improvement to an existing product it cannot be reengineered to run as a servlet within a Servlet Engine such as Jetty or Tomcat, in fact most users will continue to use it in Gui or cmd line mode.
I don't want to reinvent the wheel so I don't want to try add this basic http support from scratch.
I am using Java 8
Can some suggest an approach.
Try Spark Java.
Add maven pom dpendency:
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.6.0</version>
</dependency>
and then you will be able to start simple web server. The example shows only get
method URI, but all HTTP methods are available.
import static spark.Spark.*;
public class HelloWorld {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World");
}
}
Check it working:
http://localhost:4567/hello
There are, of course other solutions, like NanoHttpd — the most basic implementation of HTTP server I had ever seen. Usually used in android apps.