-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorldServer.java
More file actions
44 lines (41 loc) · 1.63 KB
/
HelloWorldServer.java
File metadata and controls
44 lines (41 loc) · 1.63 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class HelloWorldServer {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", new HelloWorldHandler());
server.setExecutor(null);
System.out.println("Server started on http://localhost:8080");
server.start();
}
static class HelloWorldHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
String response = """
<!DOCTYPE html>
<html>
<head>
<title>Hello World Java App</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; margin-top: 100px; }
h1 { color: #333; font-size: 3em; }
</style>
</head>
<body>
<h1>Hello! World</h1>
<p>Java application running in Docker with Ubuntu</p>
</body>
</html>
""";
exchange.getResponseHeaders().set("Content-Type", "text/html");
exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}