Skip to content

Instantly share code, notes, and snippets.

@Soontao
Last active August 30, 2017 05:00
Show Gist options
  • Save Soontao/7f7fd035a5a2b54169843865cfd18a22 to your computer and use it in GitHub Desktop.
Save Soontao/7f7fd035a5a2b54169843865cfd18a22 to your computer and use it in GitHub Desktop.
Http Server Sample with java 8 lambda
import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
public class HttpServerSample {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", exchange -> {
String body = "hello http server";
exchange.sendResponseHeaders(200, body.length());
exchange.getResponseBody().write(body.getBytes());
exchange.getRequestBody().close();
});
server.start();
System.out.println("server started at :8080");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment