第一福利在线永久视频-分享java最简单webserver代码,只支持一个httpget请求

前端开发 请求 788浏览 139评论

分享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服务器