"Software Engineering in Startup Companies," Volume: 3  Issue:10, p. 56
 

Listing 1.
// connect to the servlet
String location = "http://www.foo.com/servlet/TestServlet";
URL testServlet = new URL( servletLocation );
URLConnection servletConnection = testServlet.openConnection();

// inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);

// Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);

// Specify the content type that we will send binary data
servletConnection.setRequestProperty
("Content-Type", "<insert favorite mime type>");

// get input and output streams on servlet
. . .

// send your data to the servlet
. . .

Listing 1.
//

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 implements java.io.Serializable
{
    // 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(String aLastName, String aFirstName, String aEmail,
                   String aCompany, String aDate, String aCourseTitle,
                   String aCourseLocation, String aExpectation)
    {
        lastName = aLastName;
        firstName = aFirstName;
        email = aEmail;
        company = aCompany;

        courseDate = java.sql.Date.valueOf(aDate);
        courseTitle = aCourseTitle;
        courseLocation = aCourseLocation;
        expectations = aExpectation;
    }

    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;
    }
}
Listing 2.
//
//  Applet client-side code to send a student object
//  to a servlet in a serialized fashion.
//
//  A POST method is sent to the servlet.
//

URL studentDBservlet = new URL( webServerStr );
URLConnection servletConnection = studentDBservlet.openConnection();

// inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);

// Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);

// Specify the content type that we will send binary data
servletConnection.setRequestProperty ("Content-Type",
"application/octet-stream");

// send the student object to the servlet using serialization
outputToServlet = new
bjectOutputStream(servletConnection.getOutputStream());

// serialize the object
outputToServlet.writeObject(theStudent);

outputToServlet.flush();
outputToServlet.close();

Listing 3.
//
//  Servlet server-side code to read a serialized
//  student object from an applet.
//
//  The servlet code handles a POST method
//
public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
           throws ServletException, IOException
{
    ObjectInputStream inputFromApplet = null;
    Student aStudent = null;
    PrintWriter out = null;
    BufferedReader inTest = null;

    try
    {
        // get an input stream from the applet
       inputFromApplet = new ObjectInputStream(request.getInputStream());

        // read the serialized student data from applet
        aStudent = (Student) inputFromApplet.readObject();

        inputFromApplet.close();

       // continue the process for registering the student object

    }
    catch(Exception e)
    {
        // handle exception
    }
}

Listing 4.
//
//  Servlet server-side code to send a serialized
//  vector of student objects to an applet.
//
//

public void sendStudentList(HttpServletResponse response, Vector
studentVector)
{
     ObjectOutputStream outputToApplet;

     try
     {
         outputToApplet = new ObjectOutputStream(response.getOutputStream());

         System.out.println("Sending student vector to applet...");
         outputToApplet.writeObject(studentVector);
         outputToApplet.flush();

         outputToApplet.close();
         System.out.println("Data transmission complete.");
     }
     catch (IOException e)
     {
       e.printStackTrace();
     }
}

Listing 5.
//
//  Applet client-side code to read a serialized
//  vector of student objects from a servlet.
//
//

// connect to the servlet
URL studentDBservlet = new URL( servletLocation );
URLConnection servletConnection = studentDBservlet.openConnection();

// Don't used a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches(false);

// Read the input from the servlet.
//
// The servlet will return a serialized vector containing
// student entries.

//
inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
studentVector = (Vector) inputFromServlet.readObject();

Content-Type: application/rtf; name="Applet2Servlet.rtf"
Content-Disposition: inline; filename="Applet2Servlet.rtf"