Use of Httpserver

Source: Internet
Author: User

Http://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/overview-summary.html

1.Package Com.sun.net.httpserver Descriptionprovides A simple high-level Http server API, which can is used to build embed Ded HTTP servers. Provides a simple high-level HTTP server API, which can is used to build embedded Http servers. Both "http" and "https" are supported. The API provides a partial implementation of RFC 2616 (HTTP 1.1) and RFC 2818 (HTTP over TLS). Any HTTP functionality is provided by this API can be implemented by application code using the API.

Programmers must implement the HttpHandler interface. This interface provides a callback which are invoked to handle incoming requests from clients. A HTTP request and its response are known as an exchange. HTTP exchanges is represented by the HttpExchange class. The HttpServer class is used to listen for incoming TCP connections and it dispatches requests on these connections to handlers which has been registered with the server.

A Minimal Http Server example is shown below:

   Class MyHandler implements HttpHandler {public       void handle (Httpexchange t) throws IOException {           InputStream are = T.getrequestbody ();           Read (IS); // .. Read the request body           String response = "This is the response";           T.sendresponseheaders (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 ();   

The example above creates a simple httpserver which uses the calling application thread to invoke the handle () method for Incoming HTTP requests directed to Port 8000, and to the path/applications/myapp/.

The HttpExchange class encapsulates everything an application needs to process incoming requests and to generate appropriate RESP Onses.

Registering a handler with a httpserver creates a HttpContext object and Filter objects can is added to the returned context. Filters is used to perform automatic pre-and post-processing of exchanges before they is passed to the exchange handler .

For sensitive information, a HttpsServer can is used to process "https" requests secured by the SSL or TLS protocols. A Httpsserver must is provided with a HttpsConfigurator object, which contains an initialized SSLContext . Httpsconfigurator can used to configure the cipher suites and other SSL operating parameters. A Simple Example Sslcontext could is created as follows:

   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);   

In the example above, a keystore file called "Testkeys", created with the Keytool utility is used as a certificate store F or client and server certificates. The following code shows how the Sslcontext are then used in a httpsconfigurator and how the Sslcontext and Httpsconfigurat Or is linked to the httpsserver.

    Server.sethttpsconfigurator (New Httpsconfigurator (sslcontext) {public        void Configure (Httpsparameters params) { c3/>//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.        }    });   

Since:
1.6
2. Package Com.sun.net.httpserver.spi descriptionprovides A pluggable service provider interface, which allows the HTTP SE RVer implementation to is replaced with other implementations.

Use of Httpserver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.