Java 6 æä¾äºä¸ä¸ªè½»é级ç纯 Java Http æå¡å¨çå®ç°ãä¸é¢æ¯ä¸ä¸ªç®åçä¾åï¼
public static void main(String[] args) throws Exception{
ãHttpServerProvider httpServerProvider = HttpServerProvider.provider();
ãInetSocketAddress addr = new InetSocketAddress(7778);
ãHttpServer httpServer = httpServerProvider.createHttpServer(addr, 1);
ãhttpServer.createContext("/myapp/", new MyHttpHandler());
ãhttpServer.setExecutor(null);
ãhttpServer.start();
ãSystem.out.println("started");
}
static class MyHttpHandler implements HttpHandler{
ãpublic void handle(HttpExchange httpExchange) throws IOException {
ããString response = "Hello world!";
ããhttpExchange.sendResponseHeaders(200, response.length());
ããOutputStream out = httpExchange.getResponseBody();
ããout.write(response.getBytes());
ããout.close();
ã}
}
ããç¶åï¼å¨æµè§å¨ä¸è®¿é®
http://localhost:7778/myapp/