Skip to content

Instantly share code, notes, and snippets.

@A-pZ
Created June 5, 2015 07:15
Show Gist options
  • Save A-pZ/ce44e7c2d7ad090b4c37 to your computer and use it in GitHub Desktop.
Save A-pZ/ce44e7c2d7ad090b4c37 to your computer and use it in GitHub Desktop.
超絶かんたんなHTTPサーバ実装(Java6 or later)
@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