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?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
2 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Palladin007
1 year ago

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-

backlog – the socket backlog. If this value is less than or equal to zero, then a system default value is used.

And do not write “zero”, but “0”. “zero” has a different meaning.

Dultus, UserMod Light

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.