public class BasicHttpServerExample {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8500), 0);
HttpContext context = server.createContext("/");
context.setHandler(BasicHttpServerExample::handleRequest);
server.start();
}
private static void handleRequest(HttpExchange exchange) throws IOException {
String response = "Hi there!";
exchange.sendResponseHeaders(200, response.getBytes().length);//response code and length
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}