The main components are:
com.sun.net.httpserver.HttpExchange class that describes a
request and response pair,com.sun.net.httpserver.HttpHandler interface to handle
incoming requests, plus the com.sun.net.httpserver.HttpHandlers class
that provides useful handler implementations,com.sun.net.httpserver.HttpContext class that maps a URI path
to a HttpHandler,com.sun.net.httpserver.HttpServer class to listen for
connections and dispatch requests to handlers,com.sun.net.httpserver.Filter class that allows pre- and post-
processing of requests.
The com. class offers a simple
HTTP-only file server (intended for testing, development and debugging purposes
only). A default implementation is provided via the jwebserver tool.
Programmers must implement the com. interface. This interface
provides a callback which is invoked to handle incoming requests from clients.
A HTTP request and its response is known as an exchange. HTTP exchanges are
represented by the com. class.
The com. class is used to listen for incoming TCP connections
and it dispatches requests on these connections to handlers which have 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 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), 0);
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 com. class encapsulates everything an application needs to
process incoming requests and to generate appropriate responses.
Registering a handler with a HttpServer creates a com. object and
com.
objects can be added to the returned context. Filters are used to perform automatic pre- and
post-processing of exchanges before they are passed to the exchange handler.
For sensitive information, a com. can
be used to process "https" requests secured by the SSL or TLS protocols.
A HttpsServer must be provided with a
com. object, which contains an
initialized javax..
HttpsConfigurator can be used to configure the
cipher suites and other SSL operating parameters.
A simple example SSLContext could be 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 for client and server certificates. The following code shows how the SSLContext is then used in a HttpsConfigurator and how the SSLContext and HttpsConfigurator are linked to the 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.
}
});
| Modifier and Type | Interface and Description |
|---|---|
| public interface | HttpHandler
A handler which is invoked to process HTTP exchanges. |
| public interface | Request
A view of the immutable request state of an HTTP exchange. |
| Modifier and Type | Class and Description |
|---|---|
| public abstract class | Authenticator
Authenticator represents an implementation of an HTTP authentication mechanism. |
| public abstract class | BasicAuthenticator
BasicAuthenticator provides an implementation of HTTP Basic authentication. |
| public abstract class | Filter
A filter used to pre- and post-process incoming requests. |
| public class | Headers
HTTP request and response headers are represented by this class which
implements the interface
|
| public abstract class | HttpContext
|
| public abstract class | HttpExchange
This class encapsulates a HTTP request received and a response to be generated in one exchange. |
| public class | HttpHandlers
Implementations of |
| public class | HttpPrincipal
Represents a user authenticated by HTTP Basic or Digest authentication. |
| public class | HttpsConfigurator
This class is used to configure the https parameters for each incoming
https connection on a |
| public abstract class | HttpServer
This class implements a simple HTTP server. |
| public abstract class | HttpsExchange
This class encapsulates a HTTPS request received and a response to be
generated in one exchange and defines the extensions to |
| public abstract class | HttpsParameters
Represents the set of parameters for each https connection negotiated with clients. |
| public abstract class | HttpsServer
This class is an extension of |
| public class | SimpleFileServer
A simple HTTP file server and its components (intended for testing, development and debugging purposes only). |