哄女朋友睡觉故事-分享java最简单webserver代码,只支持一个httpget请求

Java高级开发 最简单 674浏览 268评论

分享java最简单webserver代码,只支持一个httpget哄女朋友睡觉故事

pack**ecom.zuidaima.simpleserver;importjava.io.IOException;importjava.io.OutputStream;importjava.net.InetSocketAddress;importcom.sun.net.httpserver.HttpExchange;importcom.sun.net.httpserver.HttpHandler;importcom.sun.net.httpserver.HttpServer;*/publicclassSimpleHttpServer{publicstaticvoidmain(String[]args)throwsException{HttpServerserver=HttpServer.create(newInetSocketAddress(8000),0);server.createContext("/zuidaima",newMyHandler());server.setExecutor(null);//createsadefaultexecutorserver.start();}staticclassMyHandlerimplementsHttpHandler{publicvoidhandle(HttpExchanget)throwsIOException{Stringresponse="";t.sendResponseHeaders(200,response.length());OutputStreamos=t.getResponseBody();os.write(response.getBytes());os.close();}}}

访问:http://localhost:8000/zuidaima

0个哄女朋友睡觉故事javajava简单web的server服务Web服务器