import module java.base; public class Main { public static void main(Stiring[] args) { var data = List.of(“apple”, “grape”); data.forEach(IO::println); } }
is not required import module java.base; public class Main { public static void main(Stiring[] args) { var data = List.of(“apple”, “grape”); for (var str : data) { println(str); } } }
main() throws Exception{ var server = new ServerSocket(8080); for (;;) { var soc = server.accept(); Thread.ofVirtual().start(() -> { try (soc; var isr = new InputStreamReader(soc.getInputStream()); var bur = new BufferedReader(isr); var w = new PrintWriter(soc.getOutputStream())) { // ヘッダーの解析 var line = bur.readLine(); if (line == null) return; // exit thread println("Connect from %s for %s".formatted(soc.getInetAddress(), line)); while (!bur.readLine().isEmpty()) {} // urlによって処理の振り分け var url = line.split(" ")[1]; var message = switch(url) { case "/hello" -> "Hello!!!"; case "/date" -> LocalDate.now(); default -> "It works!"; }; // ヘッダー出力 w.println(""" HTTP/1.1 200 OK content-type: text/html """); w.println(); // コンテンツ出力 w.println(""" <html><head><title>Hello</title></head> <body><h1>Hello</h1>%s</body></html> """.formatted(message)); } catch (IOException ex) { throw new UncheckedIOException(ex); } }); } }