Java HTTP サーバー

パッケージ com.sun.net.httpserver

組み込みの HTTP サーバーの構築に使用できる、単純で高度な HTTP サーバー API を提供します。

参照: 説明

パッケージ com.sun.net.httpserver の説明

組み込みの HTTP サーバーの構築に使用できる、単純で高度な HTTP サーバー API を提供します。「HTTP」と「HTTPS」の両方がサポートされています。API は、RFC 2616 (HTTP 1.1) および RFC 2818 (HTTP over TLS) の実装の一部を提供します。この API で提供されない HTTP 機能は、API を使用してアプリケーションコードで実装できます。

プログラマは、HttpHandler インタフェースを実装する必要があります。このインタフェースは、クライアントからの着信要求を処理するために呼び出されるコールバックを提供します。HTTP 要求とその応答を交換といいます。HTTP 交換は、HttpExchange クラスによって表されます。HttpServer クラスは、着信 TCP 接続の待機に使用され、これらの接続での要求をサーバーに登録されているハンドラにディスパッチします。

最小の HTTP サーバーの例を次に示します。

   class MyHandler implements HttpHandler {
       public void handle(HttpExchange t) throws IOException {
           InputStream is = t.getRequestBody();
           read(is); // .. read the request body
           String response = "This is the response";
           t.sendResponseHeaders(200, response.length());
           OutputStream os = t.getResponseBody();
           os.write(response.getBytes());
           os.close();
       }
   }
   ...

   HttpServer server = HttpServer.create(new InetSocketAddress(8000));
   server.createContext("/applications/myapp", new MyHandler());
   server.setExecutor(null); // creates a default executor
   server.start();
   

この例では、呼び出し側アプリケーションスレッドを使用して、ポート 8000 およびパス /applications/myapp/ に転送された着信 http 要求に対して handle() メソッドを呼び出す単純な HttpServer を作成します。

HttpExchange クラスは、アプリケーションが着信要求を処理し、適切な応答を生成するために必要なものをすべてカプセル化します。

HttpServer にハンドラを登録すると HttpContext オブジェクトが作成され、返されたコンテキストに Filter オブジェクトを追加できます。交換ハンドラに渡す前に、交換の事前処理と事後処理を自動的に実行するにはフィルタを使用します。

機密情報については、HttpsServer を使用すると、SSL または TLS プロトコルでセキュリティー保護された https 要求を処理できます。HttpsServer は HttpsConfigurator オブジェクトを持つ必要があります。このオブジェクトには初期化された SSLContext が格納されます。HttpsConfigurator を使用すると、暗号化方式群やその他の SSL 動作パラメータを構成できます。次に SSLContext の簡単な例を示します。

   char[] passphrase = "passphrase".toCharArray();
   KeyStore ks = KeyStore.getInstance("JKS");
   ks.load(new FileInputStream("testkeys"), passphrase);

   KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
   kmf.init(ks, passphrase);

   TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
   tmf.init(ks);

   SSLContext ssl = SSLContext.getInstance("TLS");
   ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
   

この例では、クライアントとサーバーの証明書の証明書ストアとして、keytool ユーティリティーで作成された「testkeys」というキーストアファイルを使用しています。次のコードは、その後 HttpsConfigurator で SSLContext がどのように使用されるか、また SSLContext と HttpsConfigurator がどのように HttpsServer にリンクされるかを示しています。

    server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
        public void configure (HttpsParameters params) {

        // get the remote address if needed
        InetSocketAddress remote = params.getClientAddress();

        SSLContext c = getSSLContext();

        // get the default parameters
        SSLParameters sslparams = c.getDefaultSSLParameters();
        if (remote.equals (...) ) {
            // modify the default set for client x
        }

        params.setSSLParameters(sslparams);
        // statement above could throw IAE if any params invalid.
        // eg. if app has a UI and parameters supplied by a user.

        }
    });
   

導入されたバージョン:
1.6
Java HTTP サーバー


Copyright © 2005, 2013, Oracle and/or its affiliates. All rights reserved.