O'Reilly Book
Excerpts: Java Cookbook: Solutions and Examples for Java Developers
Web Server Java -- Servlets and JSP
by Ian F. Darwin
This excerpt is Chapter 18 from Java Cookbook: Solutions and Examples for Java Developers, published in June 2001 by O'Reilly.
This chapter covers Web Server Java, but you won't find anything
about writing CGI programs in Java here. Although it would be entirely
possible to do so, it would not be efficient. The whole notion of CGI programs
is pretty much passe. Every time a CGI program is invoked, the web server has
to create a new heavyweight process in which to run it; this is inefficient.
If it's interpreted in Java, the program has to be translated into machine
code each time; this is even more inefficient.
Today's trend is toward building functionality into the web
server: Microsoft ASP, PHP3, Java servlets, and JavaServer Pages? (JSP)
are examples of this. None of these normally requires a separate process to be
created for each request; the Java-based solutions run in a thread (see
Chapter 24) inside the web server, and the Java bytecode need only be
translated into machine code once in a long while, assuming a just-in-time
(JIT) runtime system. Naturally, this book concentrates on the Java solutions.
We'll use two examples in this chapter. Consider the task of
displaying a web page with five randomly chosen integer numbers (lottery
players love this sort of thing). The Java code you need is simple:
// Part of file netweb/servlets_jsp/FiveInts.java
Random r = new Random( );
for (int i=0; i<5; i++)
System.out.println(r.nextInt( ));
But of course you can't just run that and save its output into
an HTML file because you want each person seeing the page to get a different
set of numbers. If you wanted to mix that into a web page, you'd have to write
code to println( ) a bit of HTML. This would be a
Java servlet.
The servlet code could get messy, however, since you'd have to
escape double quotes inside strings. Worse, if the webmaster wanted to change
the HTML, he'd have to approach the programmer's sanctified source code and
plead to have it changed. Imagine if you could give the webmaster a page
containing a bit of HTML and the Java code you need, and have it magically
compiled into Java whenever the HTML was changed. Imagine no longer, says the
marketer, for that capability is here now, with JavaServer Pages.
The second example is a dictionary (list of terms); I'll present
this both as a servlet and as a JSP.
I won't talk about how you get your servlet engine installed,
nor exactly how you install your servlet. If you don't already have a servlet
engine, though, I'd recommend downloading Tomcat from http://jakarta.apache.org/. Tomcat is
the official reference implementation--so designated by Sun--for the servlet
and JSP standard. It is also (as you can infer from the URL) the official
servlet engine for the ever-popular Apache web server.
Chapter sections
First Servlet: Generating an HTML Page
Servlets: Processing Form Parameters
Cookies
Session Tracking
Generating PDF from a Servlet
HTML Meets Java: JSP
JSP Include/Forward
JavaServer Pages Using a Servlet
Simplifying Your JSP with a JavaBean
JSP Syntax Summary
Program: CookieCutter
Program: JabaDot Web News Portal