/[projects]/miscJava/WebserverTest/src/dk/thoerup/test/WebserverTest.java
ViewVC logotype

Contents of /miscJava/WebserverTest/src/dk/thoerup/test/WebserverTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1951 - (show annotations) (download)
Sat Mar 23 13:58:17 2013 UTC (11 years, 1 month ago) by torben
File size: 1821 byte(s)
Display more values from HTTP URI object
1 package dk.thoerup.test;
2
3 import java.io.IOException;
4 import java.net.HttpURLConnection;
5 import java.net.InetSocketAddress;
6 import java.net.URI;
7
8 import com.sun.net.httpserver.HttpExchange;
9 import com.sun.net.httpserver.HttpHandler;
10 import com.sun.net.httpserver.HttpServer;
11
12 public class WebserverTest {
13
14 public static class TestListener implements HttpHandler {
15
16 @Override
17 public void handle(HttpExchange http) throws IOException {
18 StringBuffer sb = new StringBuffer();
19
20 sb.append("Local: ").append( http.getLocalAddress().toString() ).append("\n");
21 sb.append("Remote: ").append( http.getRemoteAddress().toString() ).append("\n");
22 sb.append("Context path: ").append( http.getHttpContext().getPath() ) .append("\n");
23
24 sb.append("Protocol: ").append( http.getProtocol() ).append("\n");
25 sb.append("Method: ").append(http.getRequestMethod() ).append("\n");
26
27 URI uri = http.getRequestURI();
28 sb.append("URI: ").append( uri.toString() ).append("\n");
29 sb.append("URI path: ").append( uri.getPath() ).append("\n");
30 sb.append("URI query: ").append( uri.getQuery() ).append("\n");
31
32
33
34 byte bytes[] = sb.toString().getBytes();
35
36 http.getResponseHeaders().add("Content-Type", "text/plain");
37 http.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length );
38 http.getResponseBody().write(bytes);
39 http.close();
40 }
41 }
42
43 /**
44 * @param args
45 */
46 public static void main(String[] args) throws Exception {
47 HttpServer server;
48 InetSocketAddress adr = new InetSocketAddress("0.0.0.0", 9999);
49 server = HttpServer.create(adr, 20);
50 server.createContext("/", new TestListener() );
51 server.start();
52 System.out.println("Server is running");
53 while (true) {
54 Thread.sleep(10);
55 }
56
57 }
58
59 }

  ViewVC Help
Powered by ViewVC 1.1.20