There are two modules in Catalina: the connector and the container. In this topic you we will enhance the applications in the topic "A Simple Servlet Container" by writing a connector that create better request and response object. A connector compliant with Servlet 2.3 and 2.4 specfication must create instances of javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse to be passed the invoked servlet's service method.
Illustration shows the UML diagram of the classes in this topic.
Compare the diagram with the one in the topic "A Simple Container". The HttpServer class in "A Simple Container" has been broken into two classes: HttpConnector and HttpProcessor. Request has been replaced by HttpRequest, and Reponse by HttpResponse. Also more classes are used in this topic's application.
The HttpServer class in "A Simple Container" is responsible for warting for HTTP requests and creating request objects and response objects. In this topic's application, the task of waiting for HTTP requests is given to HttpConnector instance, and the task of creating request objects and response bojects is assigned to the HttpProcessor instance.
In this topic, HTTP request object is reprerented by the HttpRequest class, which implements javax.servlet.http.HttpServletRequest. An Http Request object will be cast to a HttpServletRequest instance and passed to the invoked servlet's service method. Therefore, every HttpRequest must have its fields properly populated so that the servlet can use them. Values that need to be assigned to HttpRequest object include the URL, query string, parameters, cookies and other headers, etc. Becuase the connector does not know which values will be needed by the invoked servlet, the connector must parse all values that can be obtained from HTTP request. However, parsing an HTTP request involves expensive string and other operations, and the connector can save a lot of cycles life if it passes only values that will be needed by servlet.
The HttpConnector class represents a connector responsible for creating a server socket that waits for incoming HTTP requests. This class implements the Runnable class so that it can be dedicated a thread of its own. The run method in this class is similar to the await method of HttpServer1 class, except that after a socket is obtained from the accept method of SocketServer, an HttpProcessor instance is created and its process method is called, passing the socket object.
The HttpProcess object create an instance of HttpRequest and therefore must populated fields in them. The HttpProcessor class, using its parse method, parses both request line and header in an HTTP request. The values resulting from the parsing are then assigned to the fields in the HttpProcessor objects. However, the parse method does not parse the parameters in the request body or query string. This task is left to the HttpRequest themselves. Only if the servlet needs a parameter will the query string or request body be parsed.
SocketInputStream extends the InputStream class and instance of SocketInputStream wraps the java.io.InputStream instance returned by socket's getInputStream method. The SocketInputStream provides two important methods readRequestLine and readHeader.readRequestLine returns the first line in an HTTP request, i.e. the line containing URL, method and HTTP version. Because processing byte stream from socket's input stream means reading from the first byte to the last byte(and never moves backwards), readRequestLine must be called only one and must be called before readHeader is called. readHeader is called to obtain a header name/value pair each time it is called and should be called repeatedly until all headers are read. The return value of readRequestLine is an instance of HttpRequestLine and the return value of readHeader is an HttpHeader object.