271. Introducing the com.sun.net.httpserver API
Since 2006, next to the SWS command-line tool, we have the programmatic bridge represented by the com.sun.net.httpserver
API. Practically, the goal of this API is to allow us to programmatically launch an SWS instance in a very easy way.
First, we have the SimpleFileServer
, which is the main API for creating an SWS instance via three static methods, createFileServer()
, createFileHandler()
, and createOutputFilter()
. We are especially interested in the createFileServer(InetSocketAddress addr, Path rootDirectory, SimpleFileServer.OutputLevel outputLevel)
method. As you can see, via this method, we can create an SWS instance with a given port, root directory (this should be an absolute path), and output level as follows:
HttpServer sws = SimpleFileServer.createFileServer(
new InetSocketAddress(9009),
Path.of("./docs").toAbsolutePath(),
OutputLevel.VERBOSE);
sws.start();
After running this application, an SWS...