"Developing 3-Tier Database Applications with Java Servlets," Vol. 3, Issue 2, p. 16
Listing 1: HTML code for main menu.
<!-- Main Menu Page:  index.html -->
<HTML>
<HEAD>
   <TITLE>Student Database Connection</TITLE>
</HEAD>
<BODY>

<CENTER>
<H1>
Student Database Connection (SDBC)</H1></CENTER>

<HR WIDTH="100%">
<H2>
Options</H2>

<UL>
<LI>
<A HREF="StudentRegistration.htm">Register Online!</A></LI>

<LI>
<A HREF="/servlet/StudentDBServlet">View the Student List</A></LI>
</UL>
&nbsp;
</BODY>
</HTML>

Listing 2.
<!-- Student Registration page:  StudentRegistration.html -->

<HTML>
<HEAD>
   <TITLE>Student Registration</TITLE>
</HEAD>
<BODY>

<CENTER>
<H1>
Student Registration</H1></CENTER>

<HR>
<H2>
Instructions</H2>

<OL>
<LI>
Enter your information in the fields below.</LI>

<LI>
Press the <B>Register </B>button to enter your information into the course
database.</LI>
</OL>
<FORM method="GET" action="/servlet/StudentDBServlet">
<CENTER><TABLE BORDER=0 CELLPADDING=5 WIDTH="95%" >
<TR>
<TD WIDTH="36%"><B>First Name&nbsp;</B></TD>

<TD WIDTH="50%"><INPUT type="text" name="FirstName" size="20"></TD>

<TD WIDTH="43%"><B>Last Name</B></TD>

<TD WIDTH="57%"><INPUT type="text" name="LastName" size="20"></TD>
</TR>

<TR>
<TD WIDTH="36%"><B>E-Mail&nbsp;</B></TD>

<TD WIDTH="50%"><INPUT type="text" name="Email" size="20"></TD>

<TD WIDTH="43%"><B>Company</B></TD>

<TD WIDTH="57%"><INPUT type="text" name="Company" size="20"></TD>
</TR>

<TR>
<TD WIDTH="36%"><B>Course Title</B></TD>

<TD WIDTH="50%"><SELECT name="CourseTitle" size="1"><OPTION selected value="-- Please Select A Course --">--
Please Select A Course --</OPTION>&nbsp;<OPTION value="Java Introduction">Java
Introduction</OPTION>&nbsp;<OPTION value="Java Database Apps">Java Database
Apps</OPTION>&nbsp;<OPTION value="Java Network Programming">Java Network
Programming</OPTION>&nbsp;<OPTION value="Java Distributed Computing ">Java
Distributed Computing</OPTION>&nbsp;<OPTION value="JavaBeans Introduction">JavaBeans
Introduction</OPTION>&nbsp;<OPTION value="JavaBeans for the Enterprise">JavaBeans
for the Enterprise</OPTION>&nbsp;<OPTION value="Java Servlets">Java Servlets</OPTION>&nbsp;<OPTION value="Java AWT &amp; JFC">Java
AWT &amp; JFC</OPTION>&nbsp;</SELECT></TD>

<TD WIDTH="43%"><B>Course Start Date </B><I>(yyyy-mm-dd)</I></TD>

<TD WIDTH="57%"><INPUT type="text" name="CourseStartDate" size="20"></TD>
</TR>

<TR>
<TD><B>Course Location</B></TD>

<TD><SELECT name="CourseLocation" size="1"><OPTION selected value="-- Please Select Course Location--">--
Please Select Course Location --</OPTION>&nbsp;<OPTION value="Houston, TX">Houston,
TX</OPTION>&nbsp;<OPTION value="Washington, DC">Washington, DC</OPTION>&nbsp;<OPTION value="New York City, NY">New
York City, NY</OPTION>&nbsp;<OPTION value="Los Angeles, CA">Los Angeles,
CA</OPTION>&nbsp;<OPTION value="Chicago, IL">Chicago, IL</OPTION>&nbsp;<OPTION value="Atlanta, GA">Atlanta,
GA</OPTION>&nbsp;<OPTION value="Boston, MA">Boston, MA</OPTION>&nbsp;<OPTION value="Biloxi, MS">Biloxi,
MS</OPTION>&nbsp;</SELECT></TD>

<TD></TD>

<TD></TD>
</TR>
</TABLE></CENTER>
&nbsp;
<CENTER><TABLE BORDER=0 CELLPADDING=5 WIDTH="95%" >
<TR>
<TD WIDTH="100%"><B>Course Expectations</B>&nbsp;

<P>&nbsp;<TEXTAREA rows="5" name="Expectations" cols="66"></TEXTAREA></TD>
</TR>
</TABLE></CENTER>
&nbsp;
<CENTER><INPUT type="submit" value="Register" name="Register"><INPUT type="reset" value="Reset Form" name="B2"></CENTER>
</FORM>
<HR>
<CENTER><A HREF="index.html">Return to Course Home Page</A></CENTER>

</BODY>
</HTML>

Listing 3: init() method.
//  File:  StudentDBServlet.java
//  Listing 3
//
import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import java.io.*;

import shod.register.Student;

/**
 *  This servlet provides data entry and retrieval of
 *  student data in a database.
 *
 *  @author Chad (shod) Darby,  darby@j-nine.com
 *  @version 0.6, 5 Jan 1998
 *
 */
public class StudentDBServlet extends HttpServlet
{
    // data members
    protected Connection dbConnection;
    protected PreparedStatement displayStatement;
    protected PreparedStatement registerStatement;

    protected String dbURL = "jdbc:odbc:StudentDatabase";
    protected String userID = "";
    protected String passwd = "";

    protected String CR = "\n";

    protected final int LAST_NAME_POSITION  = 1;
    protected final int FIRST_NAME_POSITION = 2;
    protected final int EMAIL_POSITION      = 3;
    protected final int COMPANY_POSITION    = 4;
    protected final int EXPECTATIONS_POSITION = 5;
    protected final int COURSE_TITLE_POSITION        = 6;
    protected final int COURSE_LOCATION_POSITION     = 7;
    protected final int COURSE_DATE_POSITION   = 8;

    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);

        // use println statements to send status messages to Web server console
        try {
            System.out.println("StudentDBServlet init: Start");

            System.out.println("StudentDBServlet init: Loading Database Driver");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            System.out.println("StudentDBServlet init: Getting a connection to - " + dbURL);
            dbConnection = DriverManager.getConnection(dbURL, userID, passwd);

            System.out.println("StudentDBServlet init: Preparing display statement");
            displayStatement =
               dbConnection.prepareStatement("select * from Students order by LastName");

            System.out.println("StudentDBServlet init: Preparing register statement");
            registerStatement =
               dbConnection.prepareStatement("insert into Students "
                 + "(LastName, FirstName, Email, Company, CourseExpectations, CourseTitle, CourseLocation, CourseStartDate)"
                 + " values (?, ?, ?, ?, ?, ?, ?, ?)");

            System.out.println("StudentDBServlet init: End");
        }
        catch (Exception e)
        {
            cleanUp();
            e.printStackTrace();
        }
    }

    public void service(HttpServletRequest request,
                        HttpServletResponse response)
           throws ServletException, IOException
    {

        String userOption = null;

        userOption = request.getParameter("Register");

        if (userOption != null)
        {
            // hidden form field "Register" was present
            registerStudent(request, response);
        }
        else
        {
            // simply display the students
            displayStudents(request, response);
        }
    }

    public void displayStudents(HttpServletRequest request,
                                HttpServletResponse response)
    {
        Student aStudent = null;

        try {
            // build the html page heading
            String htmlHead = "<html><head><title>List of Students</title></head>" + CR;

            // build the html body
            String htmlBody = "<body><center>" + CR;
            htmlBody += "<h1>Student List</h1>" + CR;
            htmlBody += "<hr></center><p>" + CR;

            // build the table heading
            String tableHead = "<center><table border width=100% cellpadding=5>" + CR;
            tableHead += "<tr>" + CR;
            tableHead += "<th> </th>" + CR;
            tableHead += "<th>Student Name</th>" + CR;
            tableHead += "<th>E-mail</th>" + CR;
            tableHead += "<th>Company</th>" + CR;
            tableHead += "<th>Course Expectations</th>" + CR;
            tableHead += "</tr>" + CR;

            // execute the query to get a list of the students
            ResultSet dataResultSet = displayStatement.executeQuery();

            // build the table body
            String tableBody = "";

            int rowNumber = 1;
            while (dataResultSet.next())
            {
                aStudent = new Student(dataResultSet);
                tableBody += aStudent.toTableString(rowNumber);
                rowNumber++;
            }

            dataResultSet.close();

            // build the table bottom
            String tableBottom = "</table></center>";

            // build html page bottom
            String htmlBottom = "</body></html>";

            // build complete html page
            htmlBody += tableHead + tableBody + tableBottom;
            htmlBody += "<p><hr>";
            htmlBody += "<center><a href=/StudentDB/index.html>Return to Course Home Page</a>";
            htmlBody += "<p><i>" + this.getServletInfo() + "</i>";
            htmlBody += "</center>";
            String htmlPage = htmlHead + htmlBody + htmlBottom;

            // now let's send this dynamic data
            // back to the browser
            PrintWriter outputToBrowser =  new PrintWriter(response.getOutputStream());
            response.setContentType("text/html");
            outputToBrowser.println(htmlPage);
            outputToBrowser.close();

        }
        catch (Exception e)
        {
            cleanUp();
            e.printStackTrace();
        }
    }

    public void registerStudent(HttpServletRequest request,
                                HttpServletResponse response)
    {
        try {
            // create a new student based on the form data
            Student aStudent = new Student(request);

            // set sql parameters
            registerStatement.setString(LAST_NAME_POSITION, aStudent.getLastName());
            registerStatement.setString(FIRST_NAME_POSITION, aStudent.getFirstName());
            registerStatement.setString(EMAIL_POSITION, aStudent.getEmail());
            registerStatement.setString(COMPANY_POSITION, aStudent.getCompany());
            registerStatement.setString(EXPECTATIONS_POSITION, aStudent.getExpectations());
            registerStatement.setDate(COURSE_DATE_POSITION, aStudent.getCourseDate());
            registerStatement.setString(COURSE_TITLE_POSITION, aStudent.getCourseTitle());
            registerStatement.setString(COURSE_LOCATION_POSITION, aStudent.getCourseLocation());

            // execute sql
            registerStatement.executeUpdate();

            // build confirmation page
            String htmlPage = "<html><head><title>Confirmation Page</title></head>";

            htmlPage += "<body>";
            htmlPage += "<center><h1>Confirmation Page</h1></center><hr>";
            htmlPage += "The following information was entered successfully";
            htmlPage += aStudent.toWebString();

            htmlPage += "<hr>";
            htmlPage += "<center><a href=/StudentDB/index.html>Return to Home Page</a> | ";
            htmlPage += "<a href=/servlet/StudentDBServlet>View Student List</a>";
            htmlPage += "<p><i>" + this.getServletInfo() + "</i>";
            htmlPage += "</center></body></html>";

            // now let's send this dynamic data
            // back to the browser
            PrintWriter outputToBrowser =  new PrintWriter(response.getOutputStream());

            response.setContentType("text/html");
            outputToBrowser.println(htmlPage);
            outputToBrowser.close();
        }
        catch (Exception e)
        {
            cleanUp();
            e.printStackTrace();
        }
    }

    public void cleanUp()
    {
        try {
            System.out.println("Closing database connection");
            dbConnection.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public void destroy()
    {
        System.out.println("StudentDBServlet: destroy");
        cleanUp();
    }

    public String getServletInfo()
    {
        return "<i>Student Registration Servlet, v.06</i>";
    }
}

Listing 4: Student class.
//  File:  Student.java
//  Listing 4
//
package shod.register;

import java.sql.*;
import javax.servlet.http.*;

/**
 *  The Student class has data members to describe
 *  a student. String methods are available to
 *  display the data members to the console or Web page.
 *
 *  @author Chad (shod) Darby,  darby@j-nine.com
 *  @version 0.6, 5 Jan 1998
 *
 */
public class Student
{
    // data members
    protected String lastName;
    protected String firstName;
    protected String company;
    protected String email;
    protected String courseTitle;
    protected String courseLocation;
    protected String expectations;
    protected java.sql.Date courseDate;

    protected final String CR = "\n";     // carriage return

    // constructors
    public Student()
    {
    }

    public Student(HttpServletRequest request)
    {
        lastName = request.getParameter("LastName");
        firstName = request.getParameter("FirstName");
        email = request.getParameter("Email");
        company = request.getParameter("Company");

        String dateString = request.getParameter("CourseStartDate");
        courseDate = java.sql.Date.valueOf(dateString);

        courseTitle = request.getParameter("CourseTitle");
        courseLocation = request.getParameter("CourseLocation");
        expectations = request.getParameter("Expectations");
    }

    public Student(ResultSet dataResultSet)
    {

        try {
            // assign data members
            lastName = dataResultSet.getString("LastName");
            firstName = dataResultSet.getString("FirstName");
            email = dataResultSet.getString("Email");
            company = dataResultSet.getString("Company");
            expectations = dataResultSet.getString("CourseExpectations");
            courseTitle = dataResultSet.getString("CourseTitle");
            courseLocation = dataResultSet.getString("CourseLocation");
            courseDate = dataResultSet.getDate("CourseStartDate");
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    //  accessors
    public String getLastName()
    {
        return lastName;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public String getEmail()
    {
        return email;
    }

    public String getCompany()
    {
        return company;
    }

    public String getExpectations()
    {
        return expectations;
    }

    public String getCourseTitle()
    {
        return courseTitle;
    }

    public String getCourseLocation()
    {
        return courseLocation;
    }

    public Date getCourseDate()
    {
        return courseDate;
    }
 

    //  methods
    //  normal text string representation
    public String toString()
    {
        String replyString = "";

        replyString += "Name: " + lastName + ", " + firstName + CR;
        replyString += "E-mail: " + email + CR;
        replyString += "Company: " + company  + CR;
        replyString += "Course Expectations: " + expectations + CR;
        replyString += "Course Title: " + courseTitle + CR;
        replyString += "Course Location: " + courseLocation + CR;
        replyString += "Course Start Date: " + courseDate + CR + CR;

        return replyString;
    }

    //  returns data as HTML formatted un-ordered list
    public String toWebString()
    {
        String replyString = "<ul>";

        replyString += "<li><B>Name:</B> " + lastName + ", " + firstName + CR;
        replyString += "<li><B>E-mail:</B> " + email + CR;
        replyString += "<li><B>Company:</B> " + company  + CR;
        replyString += "<li><B>Course Expectations:</B> " + expectations + CR;
        replyString += "<li><B>Course Title:</B> " + courseTitle + CR;
        replyString += "<li><B>Course Location:</B> " + courseLocation + CR;
        replyString += "<li><B>Course Start Date:</B> " + courseDate + CR;

        replyString += "</ul>" + CR;

        return replyString;
    }

    // returns data formatted for an HTML table row
    public String toTableString(int rowNumber)
    {
        String replyString = "";
        String tdBegin = "<td>";
        String tdEnd = "</td>" + CR;

        replyString += "<tr>" + CR;
        replyString += tdBegin + rowNumber + tdEnd;
        replyString += tdBegin + lastName + ", " + firstName + tdEnd;
        replyString += tdBegin + "<a href=mailto:" + email + "> "
                               + email + "</a>" + tdEnd;

        replyString += tdBegin + company + tdEnd;
        replyString += tdBegin + expectations + tdEnd;
        replyString += "</tr>" + CR;

        return replyString;
    }
}