forked from ebarlas/java-httpserver-vthreads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHello.java
More file actions
25 lines (20 loc) · 742 Bytes
/
Hello.java
File metadata and controls
25 lines (20 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package httpsrv;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
public class Hello {
public static void main(String[] args) throws IOException {
var body = "hello world".getBytes();
var server = HttpServer.create(new InetSocketAddress(8080), 0);
server.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
server.createContext("/").setHandler(exchange -> {
exchange.sendResponseHeaders(200, body.length);
try (var os = exchange.getResponseBody()) {
os.write(body);
}
});
server.start();
System.out.println("ready");
}
}