The org.bzdev.ejws package
Theorg.bzdev.ejws
package provides a class for
configuring instances of HttpServer
or HttpsServer
, and several classes
that are useful for implementing an embedded web server:
- The class
EmbeddedWebServer
will configure an HttpServer or HttpsServer, including setting up a thread pool. One can add instances ofHttpHandler
using methods that bind a prefix (the initial part of a URL's path) to a context, which contains an instance ofHttpHandler
, which in turn handles an HTTP request. These methods areEmbeddedWebServer.add(String,Class,Object,com.sun.net.httpserver.Authenticator,boolean,boolean,boolean)
, which creates an instance ofFileHandler
, specifying a subclass ofWebMap
by class.EmbeddedWebServer.add(String,com.sun.net.httpserver.HttpHandler,com.sun.net.httpserver.Authenticator)
, whose arguments contain an instance ofHttpHandler
.EmbeddedWebServer.add(String,String,Object,com.sun.net.httpserver.Authenticator,boolean,boolean,boolean)
, which creates an instance ofFileHandler
, specifying a subclass ofWebMap
by name.
- The class
FileHandler
, a subclass ofHttpHandler
, contains constructors that provide the the class or class name for a subclass of WebMap, an argument for that WebMap's constructor, several boolean flags, and a string giving the protocol (HTTP or HTTPS) for use in error pages. The FileHandler class will handle errors, check the HTTP ACCEPT header, etc. - The class
WebMap
is used byFileHandler
to determine how the file handler maps paths to resources. A WebMap is usually not created directly: instead a FileHandler calls WebMap.newInstance, which creates the appropriate subclass. - The subclass of
WebMap
helps process HTTP or HTTPS requests by returning an instance ofWebMap.Info
, which provides a stream for the content of the resource and some additional information.
This package also includes partial support for WAR (Web ARchive)
files and JSP (Java Server Pages). For WAR files, the
WEB-INF/web.xml file is processed to find encodings for pages,
media-types (MIME types) for file-name extensions, error pages for
particular error codes or exceptions, and a welcome-file list. The
use of the web.xml file is describe in the documentation for
WebMap
. JSP support is really minimal and is
restricted to processing error messages. This is also describe in
the documentation for WebMap
.
For a simple coding example based on code in the EPTS program, consider the case of a web server embedded in an application for providing on-line help. The first step is to create an embedded web server:
The argument 8080 is the TCP port number to use, 48 is the TCP backlog, 2 is the number of threads the server uses, and 'false' indicates that HTTP is used rather than HTTPS. The next step is to map a path to some service.EmbeddedWebServer ews = new EmbeddedWebServer(8080, 48, 2, false);
For these arguments,ews.add("/", ResourceWebMap.class, "org/bzdev/epts/manual/", null, true, false, true);
"/"
indicates the common portion of
the URL paths for the resources being added,
ResourceWebMap
.class
and the following argument
specify a mapping class that will find classes whose paths in the
application's code base start
with org/bzdev.epts/manual/
, null indicates that an
authenticator is not used, the first true
indicates that
a web.xml file should not be used , false
indicates that
the server should not display directories, and the
final true
indicates that any WEB-INF directory should
not be visible.
The sequence of statements
looks up a web map for the context "WebMap wmap = epts.getWebMap("/"); wmap.addWelcome("index.html"); webmap.addMapping("html", "text/html; charset=utf-8");
/
", configures the
web map so that "index.html" is the 'welcome' page, and configures the
web map so that files ending in the suffix "html" will have a media type
of "text/html; charset=utf-8".
Finally
will start the web server. The URLews.start();
http://localhost:8080/
can be typed into a browser to view this web page on the same computer
as the server.