APIdia Launch Partner
Top Description Constructors Methods
com.sun.net.httpserver

public abstract Class HttpServer

extends Object
Class Inheritance
Known Direct Subclasses
com.sun.net.httpserver.HttpsServer, sun.net.httpserver.HttpServerImpl
Imports
com.sun.net.httpserver.spi.HttpServerProvider, java.io.IOException, java.net.BindException, .InetSocketAddress, java.util.Arrays, .Objects, java.util.concurrent.Executor

This class implements a simple HTTP server. A 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.util.concurrent.Executor 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:

description
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:

description
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).

Since
1.6

Constructor Summary

AccessConstructor and Description
protected
HttpServer()

Constructor for subclasses to call.

Method Summary

Modifier and TypeMethod 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 HttpServer to the given address and port number.

public static HttpServer

Returns:

an instance of HttpServer
create
()

Creates a HttpServer instance which is initially not bound to any local address/port.

public static HttpServer

Returns:

an instance of HttpServer
create
(InetSocketAddress
the address to listen on, if null then bind(InetSocketAddress, int) must be called to set the address
addr
,
int
the socket backlog. If this value is less than or equal to zero, then a system default value is used
backlog
)

Create a HttpServer instance which will bind to the specified java.net.InetSocketAddress (IP address and port number).

public static HttpServer

Returns:

the HttpServer
create
(InetSocketAddress
the address to listen on, if null then bind must be called to set the address
addr
,
int
the 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 HttpServer instance with an initial context.

public abstract HttpContext

Returns:

an instance of HttpContext
createContext
(String
the root URI path to associate the context with
path
,
HttpHandler
the handler to invoke for incoming requests
handler
)

Creates a HttpContext.

public abstract HttpContext

Returns:

an instance of HttpContext
createContext
(String
the root URI path to associate the context with
path
)

Creates a HttpContext without initially specifying a handler.

public abstract InetSocketAddress

Returns:

the InetSocketAddress the server is listening on
getAddress
()

Returns the address this server is listening on

public abstract Executor

Returns:

the Executor established for this server or null if not set.
getExecutor
()

Returns this server's Executor object if one was specified with setExecutor(Executor), or null if none was specified.

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
removeContext(HttpContext
the context to remove
context
)

Removes the given context from the server.

public abstract void
setExecutor(Executor
the Executor to set, or null for default implementation
executor
)

Sets this server's java.util.concurrent.Executor object.

public abstract void
start()

Starts this server in a new background thread.

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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

HttpServerback to summary
protected HttpServer()

Constructor for subclasses to call.

Method Detail

bindback to summary
public abstract void bind(InetSocketAddress addr, int backlog) throws IOException

Binds a currently unbound HttpServer to the given address and port number. 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.

Parameters
addr:InetSocketAddress

the address to listen on

backlog:int

the socket backlog. If this value is less than or equal to zero, then a system default value is used

Exceptions
BindException:
if the server cannot bind to the requested address or if the server is already bound
NullPointerException:
if addr is null
createback to summary
public static HttpServer create() throws IOException

Creates a HttpServer instance which is initially not bound to any local address/port. The HttpServer is acquired from the currently installed HttpServerProvider. The server must be bound using bind(InetSocketAddress, int) before it can be used.

Returns:HttpServer

an instance of HttpServer

Exceptions
IOException:
if an I/O error occurs
createback to summary
public static HttpServer create(InetSocketAddress addr, int backlog) throws IOException

Create a HttpServer instance which will bind to the specified java.net.InetSocketAddress (IP address and port number). 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 HttpServerProvider

Parameters
addr:InetSocketAddress

the address to listen on, if null then bind(InetSocketAddress, int) must be called to set the address

backlog:int

the socket backlog. If this value is less than or equal to zero, then a system default value is used

Returns:HttpServer

an instance of HttpServer

Exceptions
IOException:
if an I/O error occurs
BindException:
if the server cannot bind to the requested address, or if the server is already bound
createback to summary
public static HttpServer create(InetSocketAddress addr, int backlog, String path, HttpHandler handler, Filter... filters) throws IOException

Creates an HttpServer instance with an initial context.

The server is created with an initial context that maps the URI path to the exchange handler. The initial context is created as if by an invocation of createContext(path). The filters, if any, are added to the initial context, in the order they are given. The returned server is not started so can be configured further if required.

The server instance will bind to the given java.net.InetSocketAddress.

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 HttpServerProvider.

Parameters
addr:InetSocketAddress

the address to listen on, if null then bind must be called to set the address

backlog:int

the socket backlog. If this value is less than or equal to zero, then a system default value is used

path:String

the root URI path of the context, must be absolute

handler:HttpHandler

the HttpHandler for the context

filters:Filter[]

the Filters for the context, optional

Returns:HttpServer

the HttpServer

Exceptions
IOException:
if an I/O error occurs
BindException:
if the server cannot bind to the address
IllegalArgumentException:
if path is invalid
NullPointerException:
if any of: path, handler, filters, or any element of filters, are null
Since
18
createContextback to summary
public abstract HttpContext createContext(String path, HttpHandler handler)

Creates a HttpContext. A HttpContext represents a mapping from a URI path to a exchange handler on this HttpServer. Once created, all requests received by the server for the path will be handled by calling the given handler object. The context is identified by the path, and can later be removed from the server using this with the removeContext(String) method.

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 /. If the path does not end with /, e.g., such as with /foo, then some implementations may use string prefix matching where this context path matches request paths /foo, /foo/bar, or /foobar. Others may use path prefix matching where /foo matches request paths /foo and /foo/bar, but not /foobar.

Implementation Note

By default, the JDK built-in implementation uses path prefix matching. String prefix matching can be enabled using the sun.net.httpserver.pathMatcher system property.

Parameters
path:String

the root URI path to associate the context with

handler:HttpHandler

the handler to invoke for incoming requests

Returns:HttpContext

an instance of HttpContext

Exceptions
IllegalArgumentException:
if path is invalid, or if a context already exists for this path
NullPointerException:
if either path, or handler are null
See Also
sun.net.httpserver.pathMatcher
createContextback to summary
public abstract HttpContext createContext(String path)

Creates a HttpContext without initially specifying a handler. The handler must later be specified using HttpContext#setHandler(HttpHandler). A HttpContext represents a mapping from a URI path to an exchange handler on this HttpServer. Once created, and when the handler has been set, all requests received by the server for the path will be handled by calling the handler object. The context is identified by the path, and can later be removed from the server using this with the removeContext(String) method.

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 /. If the path does not end with /, e.g., such as with /foo, then some implementations may use string prefix matching where this context path matches request paths /foo, /foo/bar, or /foobar. Others may use path prefix matching where /foo matches request paths /foo and /foo/bar, but not /foobar.

Implementation Note

By default, the JDK built-in implementation uses path prefix matching. String prefix matching can be enabled using the sun.net.httpserver.pathMatcher system property.

Parameters
path:String

the root URI path to associate the context with

Returns:HttpContext

an instance of HttpContext

Exceptions
IllegalArgumentException:
if path is invalid, or if a context already exists for this path
NullPointerException:
if path is null
See Also
sun.net.httpserver.pathMatcher
getAddressback to summary
public abstract InetSocketAddress getAddress()

Returns the address this server is listening on

Returns:InetSocketAddress

the InetSocketAddress the server is listening on

getExecutorback to summary
public abstract Executor getExecutor()

Returns this server's Executor object if one was specified with setExecutor(Executor), or null if none was specified.

Returns:Executor

the Executor established for this server or null if not set.

removeContextback 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.

Parameters
path:String

the path of the handler to remove

Exceptions
IllegalArgumentException:
if no handler corresponding to this path exists.
NullPointerException:
if path is null
removeContextback 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.

Parameters
context:HttpContext

the context to remove

Exceptions
NullPointerException:
if context is null
setExecutorback to summary
public abstract void setExecutor(Executor executor)

Sets this server's java.util.concurrent.Executor object. An Executor must be established before start() is called. All HTTP requests are handled in tasks given to the executor. If this method is not called (before start()) or if it is called with a null Executor, then a default implementation is used, which uses the thread which was created by the start() method.

Parameters
executor:Executor

the Executor to set, or null for default implementation

Exceptions
IllegalStateException:
if the server is already started
startback 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.

stopback 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 start() exits, and the method returns. Once stopped, a HttpServer cannot be re-used.

Parameters
delay:int

the maximum time in seconds to wait until exchanges have finished

Exceptions
IllegalArgumentException:
if delay is less than zero