Created
June 5, 2015 07:15
-
-
Save A-pZ/ce44e7c2d7ad090b4c37 to your computer and use it in GitHub Desktop.
超絶かんたんなHTTPサーバ実装(Java6 or later)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Log4j2 | |
public class SampleServer implements HttpHandler { | |
public static void main(String[] args) throws Exception { | |
SampleServer sample = new SampleServer(); | |
int port = 80; | |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); | |
server.createContext("/" ,sample ); | |
server.start(); | |
} | |
public void handle(HttpExchange exchange) throws IOException { | |
OutputStream out = exchange.getResponseBody(); | |
log.debug("Remote : {}" , exchange.getRemoteAddress()); | |
log.debug("Method : {}" , exchange.getRequestMethod()); | |
byte[] b = (new String("ok")).getBytes(); | |
exchange.sendResponseHeaders(200, b.length); | |
out.write(b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment