his chapter presents an overview of servlets. Servlets are small programs that execute on
the server side of a web connection. Just as applets dynamically extend the functionality
of a web browser, servlets dynamically extend the functionality of a web server. The
topic of servlets is quite large, and it is beyond the scope of this chapter to cover it all. Instead,
we will focus on the core concepts, interfaces, and classes, and develop several examples.
In order to understand the advantages of servlets, you must have a basic understanding of
how web browsers and servers cooperate to provide content to a user. Consider a request
for a static web page. A user enters a Uniform Resource Locator (URL) into a browser. The
browser generates an HTTP request to the appropriate web server. The web server maps
this request to a specific file. That file is returned in an HTTP response to the browser. The
HTTP header in the response indicates the type of the content. The Multipurpose Internet
Mail Extensions (MIME) are used for this purpose. For example, ordinary ASCII text has a
MIME type of text/plain. The Hypertext Markup Language (HTML) source code of a web
page has a MIME type of text/html.
Now consider dynamic content. Assume that an online store uses a database to store
information about its business. This would include items for sale, prices, availability, orders,
and so forth. It wishes to make this information accessible to customers via web pages. The
contents of those web pages must be dynamically generated to reflect the latest information
in the database.
In the early days of the Web, a server could dynamically construct a page by creating a
separate process to handle each client request. The process would open connections to one
or more databases in order to obtain the necessary information. It communicated with the
web server via an interface known as the Common Gateway Interface (CGI). CGI allowed
the separate process to read data from the HTTP request and write data to the HTTP response.
A variety of different languages were used to build CGI programs. These included C, C++,
and Perl.
However, CGI suffered serious performance problems. It was expensive in terms of
processor and memory resources to create a separate process for each client request. It was
also expensive to open and close database connections for each client request. In addition,
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks