1 | import com.sun.net.httpserver.HttpExchange;
|
2 | import com.sun.net.httpserver.HttpHandler;
|
3 | import com.sun.net.httpserver.HttpServer;
|
4 |
|
5 | import java.io.IOException;
|
6 | import java.io.OutputStream;
|
7 | import java.io.PrintWriter;
|
8 | import java.io.StringWriter;
|
9 | import java.net.InetSocketAddress;
|
10 |
|
11 | public class main {
|
12 | private static HttpServer server;
|
13 | private static int port = 8080;
|
14 |
|
15 | public static void main(String[] args) throws Exception {
|
16 | server = HttpServer.create(new InetSocketAddress(port), 0);
|
17 | createHttpContext("/", new MainContext());
|
18 | server.setExecutor(null); // creates a default executor
|
19 | server.start();
|
20 | System.out.println("HTTP server started (listening on port " + port + "!)");
|
21 | }
|
22 |
|
23 | private static void createHttpContext(String path, HttpHandler handler) {
|
24 | server.createContext(path, new SafeHttpHandler(handler));
|
25 | }
|
26 |
|
27 | public static class SafeHttpHandler implements HttpHandler {
|
28 | private HttpHandler handler;
|
29 |
|
30 | public SafeHttpHandler(HttpHandler handler) {
|
31 | this.handler = handler;
|
32 | }
|
33 |
|
34 | public void handle(HttpExchange exchange) throws IOException {
|
35 | System.out.println("[web server] Handling request: " + exchange.getRequestURI());
|
36 | try {
|
37 | handler.handle(exchange);
|
38 | } catch (Throwable e) {
|
39 | String stackTrace = getStackTrace(e);
|
40 | sendServerError(exchange, stackTrace);
|
41 | System.out.println(stackTrace);
|
42 | } finally {
|
43 | exchange.close();
|
44 | System.out.println("[web server] Done handling request: " + exchange.getRequestURI());
|
45 | }
|
46 | }
|
47 | }
|
48 |
|
49 | public static String getStackTrace(Throwable e) {
|
50 | StringWriter stringWriter = new StringWriter();
|
51 | e.printStackTrace(new PrintWriter(stringWriter));
|
52 | return stringWriter.toString();
|
53 | }
|
54 |
|
55 | public static void sendHtml(HttpExchange exchange, String content) throws IOException {
|
56 | int responseCode = 200;
|
57 | String contentType = "text/html";
|
58 | sendHttpTextResponse(exchange, responseCode, content, contentType);
|
59 | }
|
60 |
|
61 | public static void sendHttpTextResponse(HttpExchange exchange, int responseCode, String content, String contentType) throws IOException {
|
62 | byte[] bytes = content.getBytes("UTF-8");
|
63 | String header = contentType + "; charset=utf-8";
|
64 | System.out.println("Sending content type: " + header);
|
65 | exchange.getResponseHeaders().set("Content-Type", header);
|
66 | exchange.sendResponseHeaders(responseCode, bytes.length);
|
67 | OutputStream os = exchange.getResponseBody();
|
68 | os.write(bytes);
|
69 | os.close();
|
70 | }
|
71 |
|
72 | public static void sendServerError(HttpExchange exchange, String response) throws IOException {
|
73 | sendHttpTextResponse(exchange, 500, response, "text/plain");
|
74 | }
|
75 |
|
76 | public static class MainContext implements HttpHandler {
|
77 | public void handle(HttpExchange httpExchange) throws IOException {
|
78 | sendHtml(httpExchange, "Hello world! <a href='http://javax.tinybrain.de'>JavaX.</a>");
|
79 | }
|
80 | }
|
81 | }
|