Skip to content

Instantly share code, notes, and snippets.

@lsiu
Created April 4, 2019 14:35
Show Gist options
  • Save lsiu/979645f54d4af85b446a4a01cd2c32eb to your computer and use it in GitHub Desktop.
Save lsiu/979645f54d4af85b446a4a01cd2c32eb to your computer and use it in GitHub Desktop.
HttpServer from JDK since 1.6
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
public class WebApp {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(9394), 0);
server.createContext("/ping").setHandler(h -> {
final String response = "pong!";
h.sendResponseHeaders(200, response.getBytes().length);
try (OutputStream os = h.getResponseBody()) {
os.write(response.getBytes());
}
});
server.createContext("/shutdown").setHandler(h -> server.stop(0));
server.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment