-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWeb.java
More file actions
29 lines (25 loc) · 940 Bytes
/
Copy pathWeb.java
File metadata and controls
29 lines (25 loc) · 940 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
26
27
28
29
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class Web {
public static void main(String[] arg) throws Exception {
System.out.println("Listen: 8080.");
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/hello", new TestHandler());
server.start();
}
static class TestHandler implements HttpHandler{
@Override
public void handle(HttpExchange exchange) throws IOException {
System.out.println("/hello");
String response = "hello world";
exchange.sendResponseHeaders(200, 0);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}