What exactly happens in the following lines of code?
public static void main(String[] args) throws Exception { System.out.println("Start"); HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); server.createContext("/test", new MyHttpHandler()); server.setExecutor(null); server.start(); System.out.println("Finished"); } static class MyHttpHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { Set<String> keys = exchange.getRequestHeaders().keySet(); String response = "This is the response"; byte[] responsebytes = response.getBytes(UTF_8); exchange.sendResponseHeaders(200, responsebytes.length); OutputStream os = exchange.getResponseBody(); os.write(responsebytes); os.close(); } }
I understand the System.out.println and normal string initialization but
What is the zero at the end of HTTPServer.create(… , 0); used for?
Who can research and read is clearly in the advantage:
https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html#create-java.net.InetSocketAddress-int-
And do not write “zero”, but “0”. “zero” has a different meaning.
The system standard is then simply used. This is also explained in the documentation.
https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html#create-java.net.InetSocketAddress-int-
Reading documentation is essential for code.