Skip to content

Instantly share code, notes, and snippets.

@jvalentik
Created February 20, 2016 22:39
Show Gist options
  • Save jvalentik/c34cc617d6524df29181 to your computer and use it in GitHub Desktop.
Save jvalentik/c34cc617d6524df29181 to your computer and use it in GitHub Desktop.
JAVA HttpServer
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class PingPong {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/ping", new MyHandler());
server.setExecutor(null);
server.start();
}
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "pong\n";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment