-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleWebServer.java
43 lines (36 loc) · 1.49 KB
/
SimpleWebServer.java
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
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 SimpleWebServer {
public static void main(String[] args) throws Exception {
System.out.println("Hello. Simple Web Server.");
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", indexHandler);
server.createContext("/hello", mHelloHandler);
server.setExecutor(null);
server.start();
}
private static HttpHandler indexHandler = new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
byte[] response = "This is an index page.".getBytes();
httpExchange.sendResponseHeaders(200, response.length);
OutputStream os = httpExchange.getResponseBody();
os.write(response);
os.close();
}
};
private static HttpHandler mHelloHandler = new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
byte[] response = "Hello. SnowDeer!!".getBytes();
httpExchange.sendResponseHeaders(200, response.length);
OutputStream os = httpExchange.getResponseBody();
os.write(response);
os.close();
}
};
}