Summary
Add new classes and methods along with a module entry point to jdk.httpserver
to provide a minimal HTTP server that serves static files. The entry point declares the main class of the module, in this case the default server implementation, which can be invoked on the java command line using the --module/-m
option. This is the first module entry point in the JDK. Additionally, this change includes a small API for programmatic creation and customization of such a simple server.
Problem
It is currently not possible to run an out-of-the-box static web server on the command line in Java, nor with a few lines of code for that matter. An implementation of com.sun.net.httpserver.Handler
has to be provided and an instance of com.sun.net.httpserver.HttpServer
has to be configured correctly.
Solution
Introduce a command line option to configure and start a simple web server. This is facilitated by adding a module entry point to the jdk.httpserver
module. The entry point declares the un-exported implementation class as main class of the module, which can then be invoked on the java command line as such:
$ java -m jdk.httpserver
The implementation itself uses the existing public APIs HttpServer
, HttpHandler
, and Filter
in the com.sun.net.httpserver
package.
Additionally, introduce a small set of new APIs for programmatic use of the server components (i.e., server, handler, and filter) as well as for customized and convenient request handling.
Specification
The module entry point with its command line options is documented in the module summary of jdk.httpserver
(with links to it in the com.sun.net.httpserver
package summary and the SimpleFileServer
class-level documentation).
+ * The {@link com.sun.net.httpserver.SimpleFileServer} class offers a simple
+ * HTTP file server (intended for testing, development and debugging purposes
+ * only). A default implementation is provided via the <a id="entry-point"></a>
+ * main entry point of the {@code jdk.httpserver} module, which can be used on
+ * the command line as such:
+ * <pre>{@code
+ * Usage: java -m jdk.httpserver [-b bind address] [-p port] [-d directory]
+ * [-o none|info|verbose] [-h to show options]
+ * Options:
+ * -b, --bind-address - Address to bind to. Default: 127.0.0.1 or ::1 (loopback).
+ * For all interfaces use "-b 0.0.0.0" or "-b ::".
+ * -d, --directory - Directory to serve. Default: current directory.
+ * -o, --output - Output format. none|info|verbose. Default: info.
+ * -p, --port - Port to listen on. Default: 8000.
+ * -h, -?, --help - Print this help message.
Summary of new classes and methods in jdk.httpserver/com.sun.net.httpserver
:
- Filter: 1 new static factory method
- Headers: 1 new constructor, 2 new static factory methods
- HttpExchange: implements new Request interface
- HttpsExchange: implements new Request interface
- HttpHandlers: new class with 2 static factory methods for handler creation
- HttpServer: 1 new static factory method
- HttpsServer: 1 new static factory method
- Request: new interface with 3 abstract methods and 1 default method
- SimpleFileServer: new class with 3 static factory methods
- SimpleFileServer.OutputLevel: new nested Enum class
A zip containing the API documentation changes as specdiff is attached.
- csr of
-
JDK-8245095 Implementation of JEP 408: Simple Web Server
-
- Resolved
-
- relates to
-
JDK-8277460 Add jwebserver tool
-
- Closed
-