Skip to content

Instantly share code, notes, and snippets.

@sjyun
Created May 7, 2014 07:20
Show Gist options
  • Save sjyun/cab188a22a755914ce90 to your computer and use it in GitHub Desktop.
Save sjyun/cab188a22a755914ce90 to your computer and use it in GitHub Desktop.
package test0911;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class SimpleServer {
private static final int PORT = 7080;
public static void main(String ar[]) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0);
server.createContext("/home", new WebserverHandler());
server.start();
System.out.println("started");
}
static class WebserverHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
System.out.println("accepted");
String response = "web server test";
t.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
System.out.println("complete");
os.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment