| Previous | Table of Contents | Next |
Servlets, Suns latest contribution for enhancing the capabilities of a Web server, are small Java programs that run on a Web server. The name servlets is play on words because servlets are to a Web server as applets are to a Web browser. Servlets replace the need for other server-side programming paradigms with a Java-specific solution. Web servers began as a mechanism for sending static Web documents to a Web browser using a simple protocol, HTTP. Before long, people began looking for ways to extend the capabilities of the Web browser so that more complex forms of elements and objects such as date validation fields and spreadsheet objects could be displayed. At the same time, several technologies emerged for adding program logic on the server.
This chapter shows how typical server-side programming works and compares it to the process of servlet creation and use. This comparison provides you with a basis for understanding how other server-side technologies provide Web client services and at the same time provides an overview of how to use servlets to perform the same tasks in Java. Chapter 7, Programming Servlets, covers the features and use of servlets in greater detail.
Many technologies for programming on the server have emerged. The most common technologies include the following:
All of these technologies enable developers to create complete, distributed applications in which the user interface logic is displayed in the Web browser and the business logic is managed in server programs. Enhanced HTML capabilities for developing complete (but rudimentary) user interfaces for the Web browser and the ability to build complex business logic via CGI and other technologies on the Web server clearly changed the way enterprise applications are written for a large number of systems.
Rather than creating a single monolithic application, we define smaller applications that provide a finite number of functions such as validating a credit card or retrieving a product description from a database. These smaller applications work together to perform the functions of a larger enterprise system. Each of these applications can reside on a single server, or they can be distributed throughout the network. This flexibility allows better load balancing, performance tuning, and maintenance of each individual component. For instance, you can upgrade one component such as an application that provides the user with a menu of options without having to change the rest of the application components.
The next section describes how a CGI script can be used to process Web client requests. This discussion forms a basis for comparing CGI and servlets.
While each of the server technologies is slightly different in how it performs its tasks, the CGI process illustrates the basic purpose and function of server-side logic. CGI programs can be used to dynamically generate Web pages, as in this simple example using the Perl language:
#!/bin/perl # Send the output mime type to the server to know what output to handle print Content-type: text/html\n\n; print <HTML>\n; print <HEAD><TITLE>Hello World</TITLE></HEAD>\n; print <BODY>\n; print <H1>Hello World</H1>\n; print </BODY></HTML>\n; exit;
Depending on how your Web server is configured, from a Web browser you could type:
<HTTP://www.myserver.com/cgi-scripts/helloworld.pl>
to access this program. Typically, CGI programs are used to process HTML forms and then return dynamic HTML; however, CGI can be used to input and output any type of data. Figure 6.1 shows a simple HTML form that is processed with a CGI script.
Figure 6.1 HTML form.
After the user enters values in the form, he presses the Submit button. The values are sent to the CGI script as the FORM action. The CGI script processes the values sent to it and then dynamically returns an HTML page thanking the user for the entry.
Here is the HTML for the page:
<html>
<head>
<title>Book Review</title>
</head>
<body>
<H1>Book Review</H1>
<form action=<HTTP://servername/cgi-bin/bookreview.pl> method=POST>
What is your first name?
<input type=text name=fname>
<BR><BR>Check if you are planning to write a servlet
<input type=checkbox name=writeservlet>
<BR><BR>What was your favorite chapter so far?<BR>
<BR>Chp. 1<input type=radio name=favchap value=1>
<BR>Chp. 2<input type=radio name=favchap value=2>
<BR>Chp. 3<input type=radio name=favchap value=3>
<BR>Chp. 4<input type=radio name=favchap value=4>
<BR>Chp. 5<input type=radio name=favchap value=5>
<BR>Chp. 6<input type=radio name=favchap value=6>
<BR><BR><input type=submit><input type=reset>
</form>
</body>
</html>
Notice that each form element has a unique name and value. When the user presses the Submit button, the form data is packaged up and sent to the CGI program as a request. The CGI program parses the request and processes each of the form elements values. Then the CGI program typically returns another HTML document to the Web browser. Figure 6.2 shows what a response might look like after the preceding form is submitted.
| Previous | Table of Contents | Next |