< prev index next >

src/jdk.httpserver/share/classes/com/sun/net/httpserver/package-info.java

Print this page




  41    <p>
  42    A minimal Http server example is shown below:
  43    <blockquote><pre>
  44    class MyHandler implements HttpHandler {
  45        public void handle(HttpExchange t) throws IOException {
  46            InputStream is = t.getRequestBody();
  47            read(is); // .. read the request body
  48            String response = "This is the response";
  49            t.sendResponseHeaders(200, response.length());
  50            OutputStream os = t.getResponseBody();
  51            os.write(response.getBytes());
  52            os.close();
  53        }
  54    }
  55    ...
  56 
  57    HttpServer server = HttpServer.create(new InetSocketAddress(8000));
  58    server.createContext("/applications/myapp", new MyHandler());
  59    server.setExecutor(null); // creates a default executor
  60    server.start();
  61    </blockquote></pre>
  62    <p>The example above creates a simple HttpServer which uses the calling
  63    application thread to invoke the handle() method for incoming http
  64    requests directed to port 8000, and to the path /applications/myapp/.
  65    <p>
  66    The {@link com.sun.net.httpserver.HttpExchange} class encapsulates everything an application needs to
  67    process incoming requests and to generate appropriate responses.
  68    <p>
  69    Registering a handler with a HttpServer creates a {@link com.sun.net.httpserver.HttpContext} object and
  70    {@link com.sun.net.httpserver.Filter}
  71    objects can be added to the returned context. Filters are used to perform automatic pre- and
  72    post-processing of exchanges before they are passed to the exchange handler.
  73    <p>
  74    For sensitive information, a {@link com.sun.net.httpserver.HttpsServer} can
  75    be used to process "https" requests secured by the SSL or TLS protocols.
  76    A HttpsServer must be provided with a
  77    {@link com.sun.net.httpserver.HttpsConfigurator} object, which contains an
  78    initialized {@link javax.net.ssl.SSLContext}.
  79    HttpsConfigurator can be used to configure the
  80    cipher suites and other SSL operating parameters.
  81    A simple example SSLContext could be created as follows:
  82    <blockquote><pre>
  83    char[] passphrase = "passphrase".toCharArray();
  84    KeyStore ks = KeyStore.getInstance("JKS");
  85    ks.load(new FileInputStream("testkeys"), passphrase);
  86 
  87    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
  88    kmf.init(ks, passphrase);
  89 
  90    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
  91    tmf.init(ks);
  92 
  93    SSLContext ssl = SSLContext.getInstance("TLS");
  94    ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
  95    </blockquote></pre>
  96    <p>
  97    In the example above, a keystore file called "testkeys", created with the keytool utility
  98    is used as a certificate store for client and server certificates.
  99    The following code shows how the SSLContext is then used in a HttpsConfigurator
 100    and how the SSLContext and HttpsConfigurator are linked to the HttpsServer.
 101    <blockquote><pre>
 102     server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
 103         public void configure (HttpsParameters params) {
 104 
 105         // get the remote address if needed
 106         InetSocketAddress remote = params.getClientAddress();
 107 
 108         SSLContext c = getSSLContext();
 109 
 110         // get the default parameters
 111         SSLParameters sslparams = c.getDefaultSSLParameters();
 112         if (remote.equals (...) ) {
 113             // modify the default set for client x
 114         }
 115 
 116         params.setSSLParameters(sslparams);
 117         // statement above could throw IAE if any params invalid.
 118         // eg. if app has a UI and parameters supplied by a user.
 119 
 120         }
 121     });
 122    </blockquote></pre>
 123    <p>
 124    @since 1.6
 125  */
 126 @jdk.Exported
 127 package com.sun.net.httpserver;


  41    <p>
  42    A minimal Http server example is shown below:
  43    <blockquote><pre>
  44    class MyHandler implements HttpHandler {
  45        public void handle(HttpExchange t) throws IOException {
  46            InputStream is = t.getRequestBody();
  47            read(is); // .. read the request body
  48            String response = "This is the response";
  49            t.sendResponseHeaders(200, response.length());
  50            OutputStream os = t.getResponseBody();
  51            os.write(response.getBytes());
  52            os.close();
  53        }
  54    }
  55    ...
  56 
  57    HttpServer server = HttpServer.create(new InetSocketAddress(8000));
  58    server.createContext("/applications/myapp", new MyHandler());
  59    server.setExecutor(null); // creates a default executor
  60    server.start();
  61    </pre></blockquote>
  62    <p>The example above creates a simple HttpServer which uses the calling
  63    application thread to invoke the handle() method for incoming http
  64    requests directed to port 8000, and to the path /applications/myapp/.
  65    <p>
  66    The {@link com.sun.net.httpserver.HttpExchange} class encapsulates everything an application needs to
  67    process incoming requests and to generate appropriate responses.
  68    <p>
  69    Registering a handler with a HttpServer creates a {@link com.sun.net.httpserver.HttpContext} object and
  70    {@link com.sun.net.httpserver.Filter}
  71    objects can be added to the returned context. Filters are used to perform automatic pre- and
  72    post-processing of exchanges before they are passed to the exchange handler.
  73    <p>
  74    For sensitive information, a {@link com.sun.net.httpserver.HttpsServer} can
  75    be used to process "https" requests secured by the SSL or TLS protocols.
  76    A HttpsServer must be provided with a
  77    {@link com.sun.net.httpserver.HttpsConfigurator} object, which contains an
  78    initialized {@link javax.net.ssl.SSLContext}.
  79    HttpsConfigurator can be used to configure the
  80    cipher suites and other SSL operating parameters.
  81    A simple example SSLContext could be created as follows:
  82    <blockquote><pre>
  83    char[] passphrase = "passphrase".toCharArray();
  84    KeyStore ks = KeyStore.getInstance("JKS");
  85    ks.load(new FileInputStream("testkeys"), passphrase);
  86 
  87    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
  88    kmf.init(ks, passphrase);
  89 
  90    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
  91    tmf.init(ks);
  92 
  93    SSLContext ssl = SSLContext.getInstance("TLS");
  94    ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
  95    </pre></blockquote>
  96    <p>
  97    In the example above, a keystore file called "testkeys", created with the keytool utility
  98    is used as a certificate store for client and server certificates.
  99    The following code shows how the SSLContext is then used in a HttpsConfigurator
 100    and how the SSLContext and HttpsConfigurator are linked to the HttpsServer.
 101    <blockquote><pre>
 102     server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
 103         public void configure (HttpsParameters params) {
 104 
 105         // get the remote address if needed
 106         InetSocketAddress remote = params.getClientAddress();
 107 
 108         SSLContext c = getSSLContext();
 109 
 110         // get the default parameters
 111         SSLParameters sslparams = c.getDefaultSSLParameters();
 112         if (remote.equals (...) ) {
 113             // modify the default set for client x
 114         }
 115 
 116         params.setSSLParameters(sslparams);
 117         // statement above could throw IAE if any params invalid.
 118         // eg. if app has a UI and parameters supplied by a user.
 119 
 120         }
 121     });
 122    </pre></blockquote>
 123 
 124    @since 1.6
 125  */
 126 @jdk.Exported
 127 package com.sun.net.httpserver;
< prev index next >