HttpServer is bound to an IP address
and port number and listens for incoming TCP connections from clients on this address.
The sub-class HttpsServer implements a server which handles HTTPS requests.
One or more HttpHandler objects must be associated with a server
in order to process requests. Each such HttpHandler is registered with
a root URI path which represents the location of the application or service
on this server. The mapping of a handler to a HttpServer is
encapsulated by a HttpContext object. HttpContexts are created by
calling createContext(String, HttpHandler).
Any request for which no handler can be found is rejected with a 404 response.
Management of threads can be done external to this object by providing a
java. object. If none is provided a default
implementation is used.
Mapping request URIs to HttpContext paths
When a HTTP request is received, the appropriate HttpContext
(and handler) is located by finding the context whose path is the longest
matching prefix of the request URI's path. Paths are matched literally,
which means that the strings are compared case sensitively, and with no
conversion to or from any encoded forms. For example, given a HttpServer
with the following HttpContexts configured:
| Context | Context path |
|---|---|
| ctx1 | "/" |
| ctx2 | "/apps/" |
| ctx3 | "/apps/foo/" |
The following table shows some request URIs and which, if any context they would match with:
| Request URI | Matches context |
|---|---|
| "http://foo.com/apps/foo/bar" | ctx3 |
| "http://foo.com/apps/Foo/bar" | no match, wrong case |
| "http://foo.com/apps/app1" | ctx2 |
| "http://foo.com/foo" | ctx1 |
Note about socket backlogs
When binding to an address and port number, the application can also
specify an integer backlog parameter. This represents the maximum
number of incoming TCP connections which the system will queue internally.
Connections are queued while they are waiting to be accepted by the
HttpServer. When the limit is reached, further connections may be
rejected (or possibly ignored) by the underlying TCP implementation. Setting
the right backlog value is a compromise between efficient resource usage in
the TCP layer (not setting it too high) and allowing adequate throughput of
incoming requests (not setting it too low).
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public abstract void | bind(InetSocketAddress
the address to listen on addr, int the socket backlog. If this value is less than or equal to
zero, then a system default value is used backlog)Binds a currently unbound |
| public static HttpServer | Returns: an instance ofHttpServerCreates a |
| public static HttpServer | Returns: an instance ofHttpServerthe address to listen on, if addr, int null then
bind(InetSocketAddress, int) must be called to set
the addressthe socket backlog. If this value is less than or equal to zero,
then a system default value is used backlog)Create a |
| public static HttpServer | Returns: the HttpServerthe address to listen on, if addr, int null then
bind must be called to set the addressthe socket backlog. If this value is less than or
equal to zero, then a system default value is used backlog, String the root URI path of the context, must be absolute path, HttpHandler the HttpHandler for the context handler, Filter... the Filters for the context, optional filters)Creates an |
| public abstract HttpContext | Returns: an instance ofHttpContextthe root URI path to associate the context with path, HttpHandler the handler to invoke for incoming requests handler)Creates a |
| public abstract HttpContext | Returns: an instance ofHttpContextthe root URI path to associate the context with path)Creates a HttpContext without initially specifying a handler. |
| public abstract InetSocketAddress | Returns: theInetSocketAddress the server is listening onReturns the address this server is listening on |
| public abstract Executor | Returns: theExecutor established for this server or null if not set.Returns this server's |
| public abstract void | removeContext(String
the path of the handler to remove path)Removes the context identified by the given path from the server. |
| public abstract void | |
| public abstract void | setExecutor(Executor
the executor)Executor to set, or null for default
implementationSets this server's |
| public abstract void | |
| public abstract void | stop(int
the maximum time in seconds to wait until exchanges have finished delay)Stops this server by closing the listening socket and disallowing any new exchanges from being processed. |
| HttpServer | back to summary |
|---|---|
| protected HttpServer() Constructor for subclasses to call. | |
| bind | back to summary |
|---|---|
| public abstract void bind(InetSocketAddress addr, int backlog) throws IOException Binds a currently unbound
| |
| create | back to summary |
|---|---|
| public static HttpServer create() throws IOException Creates a
| |
| create | back to summary |
|---|---|
| public static HttpServer create(InetSocketAddress addr, int backlog) throws IOException Create a
| |
| create | back to summary |
|---|---|
| public static HttpServer create(InetSocketAddress addr, int backlog, String path, HttpHandler handler, Filter... filters) throws IOException Creates an The server is created with an initial context that maps the
URI The server instance will bind to the given
A maximum backlog can also be specified. This is the maximum number
of queued incoming connections to allow on the listening socket.
Queued TCP connections exceeding this limit may be rejected by
the TCP implementation. The HttpServer is acquired from the currently
installed
| |
| createContext | back to summary |
|---|---|
| public abstract HttpContext createContext(String path, HttpHandler handler) Creates a The path specifies the root URI path for this context. The first character of path must be '/'. The class overview describes how incoming request URIs are mapped to HttpContext instances. API Note The path should generally, but is not required to, end with Implementation Note By default, the JDK built-in implementation uses path prefix matching.
String prefix matching can be enabled using the
| |
| createContext | back to summary |
|---|---|
| public abstract HttpContext createContext(String path) Creates a HttpContext without initially specifying a handler. The handler
must later be specified using The path specifies the root URI path for this context. The first character of path must be '/'. The class overview describes how incoming request URIs are
mapped to API Note The path should generally, but is not required to, end with Implementation Note By default, the JDK built-in implementation uses path prefix matching.
String prefix matching can be enabled using the
| |
| getAddress | back to summary |
|---|---|
| public abstract InetSocketAddress getAddress() Returns the address this server is listening on
| |
| getExecutor | back to summary |
|---|---|
| public abstract Executor getExecutor() Returns this server's
| |
| removeContext | back to summary |
|---|---|
| public abstract void removeContext(String path) throws IllegalArgumentException Removes the context identified by the given path from the server. Removing a context does not affect exchanges currently being processed but prevents new ones from being accepted.
| |
| removeContext | back to summary |
|---|---|
| public abstract void removeContext(HttpContext context) Removes the given context from the server. Removing a context does not affect exchanges currently being processed but prevents new ones from being accepted.
| |
| setExecutor | back to summary |
|---|---|
| public abstract void setExecutor(Executor executor) Sets this server's
| |
| start | back to summary |
|---|---|
| public abstract void start() Starts this server in a new background thread. The background thread inherits the priority, thread group and context class loader of the caller. | |
| stop | back to summary |
|---|---|
| public abstract void stop(int delay) Stops this server by closing the listening socket and disallowing
any new exchanges from being processed. The method will then block
until all current exchange handlers have completed or else when
approximately delay seconds have elapsed (whichever happens
sooner). Then, all open TCP connections are closed, the background
thread created by
| |