Skip to content

Instantly share code, notes, and snippets.

@azizkhani
Created January 14, 2019 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azizkhani/d27506769abcac32561d0ab61b32f342 to your computer and use it in GitHub Desktop.
Save azizkhani/d27506769abcac32561d0ab61b32f342 to your computer and use it in GitHub Desktop.
HttpServer
package com.syntaxcorrect.articles.smallhttpservers;
import com.sun.net.httpserver.HttpServer;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class TheComSunNetHttpServer{
static final int port=8080;
public static void main(String args[]){
try{
HttpServer server=HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/", httpExchange ->{
byte response[]="Hello, World!".getBytes("UTF-8");
httpExchange.getResponseHeaders().add("Content-Type", "text/plain; charset=UTF-8");
httpExchange.sendResponseHeaders(200, response.length);
OutputStream out=httpExchange.getResponseBody();
out.write(response);
out.close();
});
server.start();
}
catch (Throwable tr)
{
tr.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment