Java Reference
In-Depth Information
For example, many sites simply display an “under construction” message. Clearly,
Apache is overkill for a site like this. Such a site is a candidate for a custom server that
does only one thing. Java's network class library makes writing simple servers like this
almost trivial.
Custom servers aren't useful only for small sites. High-traffic sites like Yahoo! are also
candidates for custom servers because a server that does only one thing can often be
much faster than a general-purpose server such as Apache or Microsoft IIS. It is easy to
optimize a special-purpose server for a particular task; the result is often much more
efficient than a general-purpose server that needs to respond to many different kinds
of requests. For instance, icons and images that are used repeatedly across many pages
or on high-traffic pages might be better handled by a server that read all the image files
into memory on startup and then served them straight out of RAM, rather than having
to read them off disk for each request. Furthermore, this server could avoid wasting
time on logging if you didn't want to track the image requests separately from the re‐
quests for the pages in which they were included.
Finally, Java isn't a bad language for full-featured web servers meant to compete with
the likes of Apache or IIS. Even if you believe CPU-intensive Java programs are slower
than CPU-intensive C and C++ programs (something I very much doubt is true in
modern VMs), most HTTP servers are limited by network bandwidth and latency, not
by CPU speed. Consequently, Java's other advantages, such as its half-compiled/half-
interpreted nature, dynamic class loading, garbage collection, and memory protection
really get a chance to shine. In particular, sites that make heavy use of dynamic content
through servlets, PHP pages, or other mechanisms can often run much faster when
reimplemented on top of a pure or mostly pure Java web server. Indeed, there are several
production web servers written in Java, such as the Eclipse Foundation's Jetty . Many
other web servers written in C now include substantial Java components to support the
Java Servlet API and Java Server Pages. These largely replaced traditional CGIs, ASPs,
and server-side includes, mostly because the Java equivalents are faster and less resource
intensive. I'm not going to explore these technologies here because they easily deserve
a book of their own. I refer interested readers to Jason Hunter's Java Servlet Program‐
ming (O'Reilly). However, it is important to note that servers in general and web servers
in particular are one area where Java really is competitive with C for real-world perfor‐
mance.
A Single-File Server
Our investigation of HTTP servers begins with a server that always sends out the same
file, no matter what the request. It's called SingleFileHTTPServer and is shown in
Example 9-10 . The filename, local port, and content encoding are read from the com‐
mand line. If the port is omitted, port 80 is assumed. If the encoding is omitted, ASCII
is assumed.
Search WWH ::




Custom Search