JavaServer Pages



Technically, a servlet is an object, written in Java, that is equipped to receive a request
and to construct and send back a response. Because servlets are written in Java, they
inherit all the language's power and strengths. One of these strengths is speed, as a great
deal of effort has been put into making Java perform well as a server language. Equally
important, Java is also a cross-platform technology, so it is possible to develop a servlet
under Linux, deploy it on NT, and move to Solaris when the site grows, all without
needing to change or recompile anything.
Of special interest to Web developers, Java is an intrinsically dynamic and extensible
language. This neatly eliminates the problems inherent in extending a Web server. If it
supports Java, the server can be told to load a new servlet with a minimal change to a
configuration file and without needing to recompile anything.
The servlet architecture is designed to eliminate the need to reload anything every time a
request is made. Instead, the servlet is loaded once, the first time it is needed; after that, it
stays active, turning requests into responses as quickly as the Web server can send them.
This gives servlets all the speed of Fast CGIs, with none of the hassles. In short, servlets
can completely replace CGIs, with no downside.
Servlets also have one additional advantage. Because Java was designed from the ground
up as a secure language, servlets can be run in a "secure sandbox," which will prevent
them from accessing any potentially sensitive system resources. It is certainly possible to
write a CGI that has no security problems, and there are tools to assist in this endeavor.
Nonetheless, many security breaches on Web sites happen through insecure CGIs.
The next logical step would be to build a templating system on top of servlets. However,
the Art Technology Group (ATG) had an even better idea. Instead of writing a servlet
that reads a file, figures out what to do based on special tags, and then does it, why not
translate the special tags directly into Java and then compile and run the Java code? This
would mean that the first time a page was requested, it would take a little longer to do the
conversion, but the second and subsequent requests would be as fast as a servlet. This
revolutionary concept, called page compilation, was introduced in ATG's application
server, called Dynamo. Sun was so impressed by the concept that it licensed the
technology for inclusion in its own Java Web Server. The JHTML model is shown in

Figure 1.7
.