Java Reference
In-Depth Information
D The servlet gets a writer, which prints output that will be returned to the user.
E The writer is used to print HTML text that is returned to the user.
Stumbling into the Magic Servlet trap
In better designs, the servlet is only an interface point between the server-side
logic and the user interface. Unfortunately, the monolithic, do-everything
server script is still the design of choice for a surprising number of Internet
sites. It's not always a servlet. This antipattern can take the form of a CGI
script, or an Oracle PL/SQL stored procedure, or an ActiveX request. In its
worst, and arguably most frequent, form, this servlet may handle the parsing
of the arguments, the back-end database connections, and the printing of the
resulting HTML page. The similarities to the Magic Pushbutton antipattern
that I started presenting to clients in 1990 are striking.
The program which follows is an example of a do-everything Java program
taken from a real-world consulting engagement. It is based on an administra-
tive program used to print all of the pages in a bulletin board. This example is
actually much cleaner than the Perl program that it replaced. It has been
stripped down to the basics for clarity, and the recognizable features have been
changed for readability (and to protect the guilty). The servlet logic is com-
pletely separated from the input HTML (which is not shown here), but the
silent viper is hiding within.
package bbs;
// Imports
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
These lines simply import the libraries that we will need. We have imported
the libraries for JDBC , Java utilities, Java servlets, and our database driver. This
sample uses DB2 drivers. Generic JDBC drivers are available as well, but may
have a slight performance penalty.
public class PostList
extends javax.servlet.http.HttpServlet
implements Serializable {
* Process incoming requests for information
* @param request encapsulates the request to the servlet
Search WWH ::

Custom Search