How Tomcat Works

The HttpServer1 Class

The HttpServer1 class in this application is similar to the HttpServer class in the simple web server application in Chapter 1. However, in this application the HttpServer1 class can serve both static resources and servlets. To request a static resource, you type a URL in the following format in your browser's Address or URL box:

http://machineName:port/staticResource

This is exactly how you requested a static resource in the web server application in Chapter 1.

To request a servlet, you use the following URL:

http://machineName:port/servlet/servletClass

Therefore, if you are using a browser locally to request a servlet called PrimitiveServlet, you enter the following URL in the browser's Address or URL box:

http://localhost:8080/servlet/PrimitiveServlet

This servlet container can serve PrimitiveServlet. However, if you invoke the other servlet, ModernServlet, the servlet container will throw an exception. At the later chapters, you will build applications that can process both.

The HttpServer1 class is presented in Listing 2.2.

Listing 2.2: The HttpServer1 Class's await method

package ex02.pyrmont;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class HttpServer1 {
/** WEB_ROOT is the directory where our HTML and other files reside.
* For this package, WEB_ROOT is the "webroot" directory under the
* working directory.
* The working directory is the location in the file system
* from where the java command was invoked.
*/
// shutdown command
private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
// the shutdown command received
private boolean     shutdown = false;
public static void main(String[] args) {
    HttpServer1 server = new HttpServer1(); server.await();
    }
public void await() {
    ServerSocket serverSocket = null;
    int port = 8080;
try {
    serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
    }catch (IOException e) {
        e.printStackTrace(); System.exit(1);
    }
    // Loop waiting for a request
    while (!shutdown) {
    Socket socket = null;
    InputStream input = null;
    OutputStream output = null;
    try {
        socket = serverSocket.accept();
        input = socket.getInputstream();
        output = socket.getOutputStream();
        // create Request object and parse
        Request request = new Request(input);
        request.parse();
        // create Response object
        Response response = new Response(output);            response.setRequest(request);
        // check if this is a request for a servlet or
        // a static resource
        // a request for a servlet begins with "/servlet/"
    if (request.getUri().startsWith("/servlet/")) {
        ServletProcessor1 processor = new ServletProcessor1();
        processor.process(request, response);
        }else {
        StaticResoureProcessor processor =
new StaticResourceProcessor();
        processor.process(request, response);
    }
    // Close the socket
    socket.close();
    //check if the previous URI is a shutdown command
    shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
    }catch (Exception e) {
    e.printStackTrace();
    System.exit(1);
}}}}

The class's await method waits for HTTP requests until a shutdown command is issued, and reminds you of the await method in Chapter 1. The difference between the await method in Listing 2.2 and the one in Chapter 1 is that in Listing 2.2 the request can be dispatched to either a StaticResourceProcessor or a ServletProcessor. The request is forwarded to the latter if the URI contains the string /servlet/.Otherwise, the request is passed to the StaticResourceProcessor instance. Notice that that part is greyed in Listing 2.2.