Building a Simple Web Server

Components of the Framework

This section will cover the major components of this framework.

Table 4.1. HTTP Server Components

HttpServerThis is the Grizzly HTTP server which can be used to create standalong HTTP programs or embed Grizzly within other application to provide HTTP services.
ServerConfigurationThis class allows developer to add custom HttpHandler implementations to the server as well as exposing JMX/monitoring features.
NetworkListenerThis is an abstraction of the Grizzly NIOTransport and Filter implementations. It also allows the enabling/disabling of HTTP-related features such as keep-alive, chunked transfer-encoding, cusom addons etc. HttpServer can support multiple NetworkListeners. Also, keep in mind that all HttpHandlers added to the ServerConfiguration will be shared across all listeners.
HttpHandlerHttpHandler is akin to javax.servlet.Servlet.
RequestRequest is similar to javax.servlet.http.HttpServletRequest
ResponseRequest is similar to javax.servlet.http.HttpServletResponse
SessionSession is similar to javax.servlet.http.HttpSession
HttpServerFilterThis Filter implementation provides the high-level HTTP request/response processing. Note: This Filter is automatically added to the FilterChain used by the NetworkListener, but if a custom chain as well as this level of HTTP processsing, this Filter will need to be added to the chain.
FileCacheFilterThis Filter provides static resource caching capabilites. Like the HttpServerFilter, if file caching is enabled, this Filter will be added automatically.
AddOnThe general interface for HttpServer addons, which suppose to extend basic HttpServer functionality.

Quick Start

To get started with the HTTP server framework you'll need to include the module in your project:

<dependencies>
    <dependency>
        <groupId>org.glassfish.grizzly</groupId>
        <artifactId>grizzly-http-server</artifactId>
        <version>2.2.10</version>
    </dependency>
</dependencies>

Once the dependencies are in place, the absolute simplest, albeit not-very-useful, server one can create is:

HttpServer server = HttpServer.createSimpleServer();
try {
    server.start();
    System.out.println("Press any key to stop the server...");
    System.in.read();
} catch (Exception e) {
    System.err.println(e);
}

This will create a Grizzly HTTP server listening on 0.0.0.0:8080 and will serve content from the directory in which the JVM was started. As stated before, while this demonstrates the ease of embedding Grizzly, it's not very useful.

Let's add a HttpHandler to server the current time.

  1 HttpServer server = HttpServer.createSimpleServer();
  2 server.getServerConfiguration().addHttpHandler(
  3     new HttpHandler() {
  4         public void service(Request request, Response response) throws Exception {
  5             final SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
  6             final String date = format.format(new Date(System.currentTimeMillis()));
  7             response.setContentType("text/plain");
  8             response.setContentLength(date.length());
  9             response.getWriter().write(date);
 10         }
 11     },
 12     "/time");
 13 try {
 14     server.start();
 15     System.out.println("Press any key to stop the server...");
 16     System.in.read();
 17 } catch (Exception e) {
 18     System.err.println(e);
 19 }

Line 2 adds a new HttpHandler (think Servlet) to service requests make to /time. Any other requests to the server will be considered requests for static content served from the direction in which the JVM was started.