Java Reference
In-Depth Information
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello Servlet</TITLE></HEAD>");
out.println("<BODY><H1>Hello Servlet World</H1></BODY>");
out.println("</HTML>");
out.close();
}
}
The next section expands on the basic example of Listing 12-1 to create a simple Login servlet.
Creating and Deploying a Login Servlet
As mentioned in Chapter 11 , the examples in Part III of this topic are geared toward building a simple
membership Web site. The Web site in the examples is based on SQL Server, using the Opta2000
JDBC driver. The Opta2000 driver is an excellent example of an efficient, modern, pure Java driver.
One reason for choosing the Opta2000 driver is to illustrate the use of a different driver, since most of
the sample code in Part II uses the JDBC-ODBC bridge. A more practical consideration is that the
JDBC-ODBC bridge is slow compared with Opta2000 and other commercial drivers. As I point out in
Part II , and illustrate in Chapter 10 , JDBC does such a great job of supporting different RDBMS systems
and drivers that using a different driver or database involves only a couple of minor changes in the code.
The HTTP server used in the examples is Apache, currently the most widely used and one of the
easiest to install. The servlet engine is Apache Tomcat; it has been chosen by Sun as the reference
implementation, it works well, and both Apache and Tomcat are available as free downloads from the
Apache Software Foundation at http://www.apache.org/ . Like Tomcat, Apache can be installed
on Linux, Windows, and most other major platforms.
Cross-
Reference
Installation and setup of Apache and Tomcat are covered in Appendix 2.
Implementing a Membership Web Site
The first step in implementing a membership Web site, obviously, is handling member logins. The Web-
site design discussed in Chapter 11 calls for a dedicated table for user names and passwords. The
design of this table is extremely simple, as shown in Table 12-1 .
Table 12-1: Login Table Containing Usernames and Passwords
UserName
Password
MemberID
garfield
lasagna
1
snoopy
peanuts
2
batman
robin
3
When the table is created, the UserName column is defined as the primary key, because this column is
used in a WHERE clause when a member logs in. The Password column is a simple VARCHAR field,
used only for validation. The MemberID column is important because all the other tables containing
member information use a MemberID column as their primary key to facilitate looking up member
information in other tables. MemberID is defined with the IDENTITY constraint so that the DBMS
automatically assigns a new, unique number to the field. The SQL CREATE statement used to create
this table is shown below:
CREATE TABLE LOGIN(
UserName VARCHAR(20) PRIMARY KEY,
 
Search WWH ::




Custom Search