Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

81
LINES

< > BotCompany Repo | #608 // HttpServerWithOnePage (unshortened JavaX example)

JavaX source code - run with: x30.jar

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  
}

Author comment

Should be self-contained and runnable by x9 just as is.

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
58 stefan Yup it is runnable. "java x9 608" 2015-05-05 18:00:16

add comment

Snippet ID: #608
Snippet name: HttpServerWithOnePage (unshortened JavaX example)
Eternal ID of this version: #608/1
Text MD5: 49ebf4e1127dde49d2b6622ad1d1c5f0
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-05-05 17:58:43
Source code size: 2988 bytes / 81 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 867 / 787
Referenced in: [show references]