// A simple web server using Java's built-in HttpServer // Examples from https://dzone.com/articles/simple-http-server-in-java were useful references import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.URI; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; interface URLHandler { String handleRequest(URI url); } class ServerHttpHandler implements HttpHandler { URLHandler handler; ServerHttpHandler(URLHandler handler) { this.handler = handler; } public void handle(final HttpExchange exchange) throws IOException { // form return body after being handled by program try { String ret = handler.handleRequest(exchange.getRequestURI()); // form the return string and write it on the browser exchange.sendResponseHeaders(200, ret.getBytes().length); OutputStream os = exchange.getResponseBody(); os.write(ret.getBytes()); os.close(); } catch(Exception e) { String response = e.toString(); exchange.sendResponseHeaders(500, response.getBytes().length); OutputStream os = exchange.getResponseBody(); os.write(response.getBytes()); os.close(); } } } public class Server { public static void start(int port, URLHandler handler) throws IOException { HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); //create request entrypoint server.createContext("/", new ServerHttpHandler(handler)); //start the server server.start(); System.out.println("Server Started! Visit http://localhost:" + port + " to visit."); } }