package dk.thoerup.test; import java.io.IOException; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.URI; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class WebserverTest { public static class TestListener implements HttpHandler { @Override public void handle(HttpExchange http) throws IOException { StringBuffer sb = new StringBuffer(); sb.append("Local: ").append( http.getLocalAddress().toString() ).append("\n"); sb.append("Remote: ").append( http.getRemoteAddress().toString() ).append("\n"); sb.append("Context path: ").append( http.getHttpContext().getPath() ) .append("\n"); sb.append("Protocol: ").append( http.getProtocol() ).append("\n"); sb.append("Method: ").append(http.getRequestMethod() ).append("\n"); URI uri = http.getRequestURI(); sb.append("URI: ").append( uri.toString() ).append("\n"); sb.append("URI path: ").append( uri.getPath() ).append("\n"); sb.append("URI query: ").append( uri.getQuery() ).append("\n"); byte bytes[] = sb.toString().getBytes(); http.getResponseHeaders().add("Content-Type", "text/plain"); http.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length ); http.getResponseBody().write(bytes); http.close(); } } /** * @param args */ public static void main(String[] args) throws Exception { HttpServer server; InetSocketAddress adr = new InetSocketAddress("0.0.0.0", 9999); server = HttpServer.create(adr, 20); server.createContext("/", new TestListener() ); server.start(); System.out.println("Server is running"); while (true) { Thread.sleep(10); } } }