Over time web servers need to provide all sorts of new services
and customizations. Rewriting programs for every new service
is obviously not convenient or practical. The traditional
approach is for a server to run scripts or programs via the
CGI (Common Gateway Interface) mechanism. The URL tells the
server where to find the script, which uses standard I/O to
write and read data on local storage and to send replies in
hypertext back over the web.
A purely Java approach is to use servlets. We have
seen in this course how applets are Java programs that run
inside the browser on the clients machine. Similarly, servlets
are specialized Java programs that a server runs on the its
machine. Servlets can do many of the same tasks as CGI scripts
such as process forms, executing I/O with local disk files,
database access, etc.
Servlets also have several advantages over CGI:
- CGI scripts run once and go away. Servlets can stay
in memory and respond to another request without additional
startup costs.
- " Servlets are run in a Java thread by the server and
are generally much faster to start up than CGI programs,
which require an operating system process
- A CGI script receives one input request, gives a response,
and then dies. Servlets can carry on a two-way conversation
or session for an indefinite period.
- A servlet can communicate with multiple clients simultaneously,
e.g. perform as a multi-player game server.
Furthermore, since servlets are modular, the system manager
and local users can develop or purchase servlets to enhance
the capabilities of their server.
A number of commercial and freeware servers run servlets.
See the list below.
We briefly return to the subject of servlets in Chapter
21 and their role in the area of Web services. However,
the servlet API, however, is beyond the scope of this course.
See the resources below for information and tutorial materials
in this area.
Another tool in the Java toolbox is the Java Server Page
(JSP). HTML pages at a server are typically static in that
the text is fixed until it is edited by hand. In a JSP page,
however, the hypertext contains specially tagged areas that
signal to the server or servlet where it can insert dynamically
created data. For example, the servlet could enter the latest
price for a product in a catalog page.
Though servlets and JSP are powerful tools for large enterprise
Web servers, our emphasis is in small, specialized servers
for custom applications such as monitoring remote devices.
Such servers can usually suffice with the techniques discussed
in this and the following chapters.