Java Reference
In-Depth Information
}
}
The Handler class—shown in Example 22-18 —is the part that knows the HTTP protocol, or
at least a small subset of it. You may notice near the middle that it parses the incoming HTTP
headers into a HashMap but does nothing with them. Here is a log of one connection with de-
bugging enabled:
Connection accepted from localhost/127.0.0.1
Request: Command GET, file /, version HTTP/1.0
hdr(Connection,Keep-Alive)
hdr(User-Agent,Mozilla/4.6 [en] (X11; U; OpenBSD 2.8 i386; Nav))
hdr(Pragma,no-cache)
hdr(Host,127.0.0.1)
hdr(Accept,image/gif, image/jpeg, image/pjpeg, image/png, */*)
hdr(Accept-Encoding,gzip)
hdr(Accept-Language,en)
hdr(Accept-Charset,iso-8859-1,*,utf-8)
Loading file //index.html
END OF REQUEST
At this stage, the server is getting ready to create an HttpServletRequest object, but it is
not sufficiently evolved to do so. This file is a snapshot of work in progress. More interesting
is the Hashtable used as a cache; to save disk I/O overhead, once a file has been read from
disk, the program does not reread it. This means you have to restart the server if you change
files; comparing the timestamps (see Getting File Information ) and reloading files if they
have changed is left as an exercise for the reader.
Example 22-18. Handler.java
public
public class
class Handler
Handler {
/** inputStream, from Viewer */
protected
protected BufferedReader is ;
/** outputStream, to Viewer */
protected
protected PrintStream os ;
/** Main program */
protected
protected WebProxy parent ;
/** The default filename in a directory. */
protected
protected final
final static
static String DEF_NAME = "/index.html" ;
/** The Hashtable used to cache all URLs we've read.
* Static, shared by all instances of Handler (one Handler per request;
Search WWH ::




Custom Search