HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

The drive to create a successful Web site has resulted in Web applications that are interactive and informative. A wealth of information is stored in corporate databases and there is a rush to publish this information on the Web. Corporations' traditional client/server applications are being edged out by Web-based applications. This occurrence is possible thanks to the universal client, the Web browser.

This article is the second in a three-part series on Java servlets. Last month (JDJ, Vol. 3, Iss. 1), I gave you an overview of the Java servlet technology and how to migrate your existing CGI scripts to Java servlets. This article will not reintroduce those concepts. I assume that you are familiar with the basics of the Java Servlet API and the Java Database Connection API.

In this article, you will learn how to build a 3-tier database application that uses Java servlets and the Java Database Connection (JDBC). You will witness the construction of each tier and understand the techniques used to create Java Servlets with database connectivity.

The Challenge
A prominent public speaker keeps track of students who attend her Internet seminars. After each seminar, she exchanges business cards with the interested students. She then enters the student data into her database program.

Instead of entering the data from each business card, she envisions a Web application that would do the work for her. At each seminar, the Web application is set up on several Web terminals. Each student registers at a Web terminal and provides their name, company, e-mail address, course title and expectations. The Web application also has the option to display an updated list of all students.

3-Tier Solution
The Web application is made up of three tiers: Web browser, servlet middleware and database server. The three tiers are illustrated in Figure 1.

Figure 1
Figure 1:

The first tier uses a Web browser to take advantage of the installed user base of this universal client. An HTML form is used for user-input and the results of the database query are returned as an HTML page. Using HTML for user-input and displaying the data lowers the requirement of the client's browser version. This Web application does not impose the requirement of a Java-enabled browser with the latest JDK patch.

The second tier is implemented with a Web server running Java servlets. The Java servlet is able to access the database and return an HTML page listing the data. Please note that Java servlets are not restricted to Sun Microsystem's Java Web Server. You can also use Java servlets with the following servers: Microsoft IIS, Netscape FastTrack and Enterprise Server and O'Reilly WebSite Professional. Servlet functionality is possible with Live Software's JRun product, http://www.livesoftware.com. Sun's Java Server page, http://jserv.javasoft.com, also has a list of servlet-enabled Web servers.

The third tier is the back-end database server. The Java servlet can access information in the database provided that a JDBC driver exists. In our situation, the public speaker's database is MS-Access so we can use the JDBC-ODBC driver that is bundled with the Java Development Kit versions 1.1 and higher.

Application Interaction
As you can see, the application is partitioned into three different tiers. Figure 2 illustrates the interaction between the different tiers of the application.

Figure 2
Figure 2:

Each step of the interaction is described below.
Step 1:
The user enters information into an HTML form. The form data is passed to the Java servlet running on the Web server.
Step 2:
The Java servlet parses the form data and constructs an SQL statement. The SQL statement is passed to the database server using the Java Database Connection (JDBC).
Step 3:
The database server executes the SQL statement and returns a result set to the Java servlet.
Step 4:
The Java servlet processes the result set and constructs an HTML page with the data. The HTML page is then returned to the user's Web browser.

Analyzing the Database Schema
Our public speaker is currently storing the student information in a MS Access database. The database contains one data table called Students. The data fields are defined in Table 1.

Table 1

Designing the Web Browser Interface
The browser interface is composed of a main menu page. This page presents the user with the option of student registration or displaying the students in the database. HTML code for the main menu is provided in Listing 1.

Student Registration Form
The students register using an HTML form. The form collects name, e-mail address, company name and other course information. A snapshot of the student registration form is given in figure 3.

Figure 3
Figure 3:

Once the user enters their information then the "Register" button sends the data to the Java servlet.

Developing the Servlet Middleware
The servlet middleware encapsulates the business logic of the application. The servlet parses the form data and constructs an SQL statement. The SQL statement is then passed to the database server. After executing the SQL statement, the database server returns a result set back to the servlet. At this time, the servlet processes the result set and constructs an HTML page for the user.

The servlet being created is called StudentDBServlet. The StudentDBServlet has methods to perform the following functions: initialization, servicing requests, displaying students and registering a student. Let's look at each of these functions in detail.

Initializing the Servlet
In the life cycle of a servlet, the init() method is called the first time the servlet is invoked. Listing 3 is the code listing for the init() method.

For the StudentDBServlet, a database connection is opened and prepared statements are created for displaying a student list and registering a student. The database connection is left open for the lifetime of the servlet. Depending on your design, you can open and close a connection for each SQL query. However, in this application the database connection is opened only once.

Servicing User Requests
Whenever a servlet is invoked the service() method is called. The service() method is the main entry point for servlets. However, if this is the first time the servlet is being invoked, then the init() method is called followed by the service() method.

The service() method in this application is used to branch the request to the appropriate method. The student registration form has a hidden field called Register. The service method checks the value of the Register field. If the value is non-null then the registerStudent() method is called. If the field does not exist on the HTML page then a null value is returned. A null value results in the execution of the displayStudents() method.

Displaying the Student List
The displayStudents() method encapsulates the business logic to access the database and display the student list. This is accomplished by using a supporting Student class. The code for the Student class is given in Listing 4.

The Student class has data members to hold information for one student. The Student class also has constructors that can create an object based on form data or a database result set. The code below demonstrates how a student's last name is accessed from the form data.

lastName = request.getParameter("LastName");

The request object is an instance of HttpServletRequest. The request object contains the form data. The form data is accessed by calling the getParameter() method and providing the name of the form field. The student registration form has a LastName field. Refer to my earlier article in JDJ for a detailed discussion of accessing form data with servlets.

The Student class has methods for accessing its data members and for providing a string representation of its data. Listing 4 contains the code listing for the methods of the Student class. The toString() method returns a normal string version of the data members. The toWebString() method returns the data as an HTML-formatted unordered list. The toTableString() method returns the data as an HTML-formatted table row. These methods are used to build the student list.

Constructing an HTML page creates the student list. In the displayStudents() method of Listing 3, the heading of the HTML page is created. Next the table heading is created to display the information as shown below.

Student Name E-mail Company Course Expectations

The servlet sends a request to the database server to get a list of students. The following SQL statement was prepared in the init() method.

select * from Students order by LastName;

The SQL statement will return a list of students in alphabetical order based on the last name. The result set is used to create the body of the HTML table. A while-loop is created to iterate through each record of the result set. The code fragment for the while-loop is:

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

Each record is used to create a new Student object. The toTableString() method is called to get a string representation of the student data. Recall that the toTableString() method returns the data as an HTML-formatted table row.

After the body of the table is constructed the result set is closed. At the bottom of the Web page, navigation links are provided to the main menu page.

A large amount of server-side processing has taken place. However, we are not finished yet. The HTML page must be returned to the Web browser. This is accomplished by opening an output stream on the response object. The response object is an instance of HttpServletResponse. The response object is used to respond to the client. The code for returning the HTML page to the user is:

PrintWriter outputToBrowser = new
PrintWriter(response.getOutputStream());
response.setContentType("text/html");
outputToBrowser.println(htmlPage);
outputToBrowser.close();

The content-type is set for HTML and the htmlPage string is returned to the browser using the println() method. Figure 4 is a sample student list that is returned by the StudentDBServlet.

Figure 4
Figure 4:

Registering A Student
The registerStudent() method creates a new Student object based on the HTML form data. The Student object is used to set the parameters on the SQL statement prepared in the init() method. The code fragment below shows how a parameter is set.

registerStatement.setString
(LAST_NAME_POSITION, aStudent.getLastName());

Once all of the parameters are set then the SQL statement is executed. After the statement is executed the new student data is successfully inserted into the database.

A confirmation page is constructed for the user. The confirmation page contains a list of the data that was successfully entered into the database. The Student.toWebString() method is called to provide an HTML string for an unordered list.

Pulling It All Together
At this point, all three tiers of the application are constructed. Collections of HTML pages represent the user interface component for the browser. The only requirement on the browser is the ability to display HTML tables. The two leading browsers available from Microsoft and Netscape easily satisfy this requirement, thus making the Web application browser-friendly.

The back-end database was developed with Microsoft Access. However, any database could have been used provided that a JDBC driver was available for the database. In our scenario, the public speaker was already tracking student data with MS Access. The Web application gave her the ability to access her legacy data and build on it.

The middleware was the critical piece of the application. The servlet middleware encapsulated the business logic and provided the "glue" between the Web browser interface and the backend database information. The database access was made possible by using a nice blend of Java-based technologies: Java Servlet API and JDBC.

Each of the components in the 3-tier application can reside on different computers. The application can easily be distributed across the network. With the worldwide reach of the Web browser, a user can enter information from a networked computer. The Java servlet middleware can reside on any servlet-enabled Web server. The servlet can, in turn, interact with any networked database server in a different location.

Future Enhancements
This 3-tier Web application allowed students to register their student information. The application also gave the option of displaying an updated student list. However, the application is by no means complete. There are a number of enhancements that can be made to it.

The application can be enhanced to display students from a specific city, company or course. A user could create a custom query to generate the student listing. Also, the application can be enhanced to allow remote database administration features such as updating and deleting student entries. Currently, the application does not perform error checking on the form data entered. Client-side JavaScript can be used to verify user data. The application does not consider the case where a student will attend multiple seminars. The application can be enhanced to hold multiple course titles for a student.

Conclusion
This article presented the basic components and techniques to build a 3-tier database application. You can use this information to quickly and easily build a Web interface to your existing corporate database. The Java servlet technology coupled with the Java Database Connection are the key components in creating a Web application that is informative and interactive.

Resources
Article Source Code Listings:
http://www.j-nine.com/pubs/dbservlets
Sun Microsystem's Java Server Page:
http://jserv.javasoft.com
Live Software, JRun 2.0
http://www.livesoftware.com

About the Author
Chád (shod) Darby is a Java consultant for J9 Consulting, www.j-nine.com. He specializes in developing server-side Java applications and database applications. In his spare time he enjoys running 10K races and half-marathons. Chád can be reached at [email protected]

	

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> 
  
</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 </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 </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> <OPTION value="Java Introduction">Java 
Introduction</OPTION> <OPTION value="Java Database Apps">Java Database 
Apps</OPTION> <OPTION value="Java Network Programming">Java Network 
Programming</OPTION> <OPTION value="Java Distributed Computing ">Java 
Distributed Computing</OPTION> <OPTION value="JavaBeans Introduction">JavaBeans 
Introduction</OPTION> <OPTION value="JavaBeans for the Enterprise">JavaBeans 
for the Enterprise</OPTION> <OPTION value="Java Servlets">
Java Servlets</OPTION> <OPTION value="Java AWT & JFC">
Java AWT & JFC</OPTION> </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> 
<OPTION value="Houston, TX">Houston, 
TX</OPTION> <OPTION value="Washington, DC">Washington, DC
</OPTION> <OPTION value="New York City, NY">New 
York City, NY</OPTION> <OPTION value="Los Angeles, CA">Los Angeles, 
CA</OPTION> <OPTION value="Chicago, IL">Chicago, IL
</OPTION> <OPTION value="Atlanta, GA">Atlanta, 
GA</OPTION> <OPTION value="Boston, MA">Boston, MA
</OPTION> <OPTION value="Biloxi, MS">Biloxi, 
MS</OPTION> </SELECT></TD> 

<TD></TD> 

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

<P> <TEXTAREA rows="5" name="Expectations" cols="66"></TEXTAREA>
</TD> 
</TR> 
</TABLE></CENTER> 
  
<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,  [email protected] 
 *  @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,  [email protected] 
 *  @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; 
    } 
}
 
  
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.