Java Q&A 
by W. David Pitt 

Listing One
public abstract interface Servlet 
{
public abstract void destroy();
public abstract javax.servlet.ServletConfig getServletConfig();
public abstract java.lang.String getServletInfo();
public abstract void init(javax.servlet.ServletConfig arg1) 
                                throws javax.servlet.ServletException;
public abstract void service(javax.servlet.ServletRequest arg1,
               javax.servlet.ServletResponse arg2) 
                   throws javax.servlet.ServletException, java.io.IOException;
}


Listing Two
package simple;
import java.io.*;
import javax.servlet.*;

public class HelloWorldServlet extends GenericServlet 
{
    public void service( javax.servlet.ServletRequest req,
                                      javax.servlet.ServletResponse res){
    try {
    PrintStream out = new PrintStream( 
    res.getOutputStream());
    out.println("Hello World");}
    catch( java.io.IOException e) {System.out.println(e); }
   }            
}       


Listing Three
/* doGet method is called when an HTML form accesses this servlet using GET
method where all information is encoded in QUERY_STRING environment variable.
*/      
public synchronized void 
doGet(HttpServletRequest httpServletRequest, 
                                HttpServletResponse httpServletResponse)
throws IOException
  {
    String  jobValue = null, 
    deptValue = null;

    // We will be returning an HTML page
    httpServletResponse.setContentType("text/html");
    // Get the output stream        
    ServletOutputStream servletOutputStream = 
                httpServletResponse.getOutputStream();

    //  Start writing the HTML output       
    htmlHeader(servletOutputStream);
    // Process the arguments
    processArguments(httpServletRequest, servletOutputStream);
    // Get Vector of Employee Objects
    Vector aVector =  employeeObjects();
    // Write Employee Objects to an HTML table
    htmlResultTable(aCollection, servletOutputStream);
    // Write ending HTML
    htmlTrailer(servletOutputStream);
    servletOutputStream.close();
}


Listing Four
/* Retrieve Employees from Servlet.
 * @return Vector
 */
public Vector getEmployeesFromServlet() {
    URL url = null;
    try{
        //  Create the URL
       String urlStr = "http://127.0.0.1:8080/servlet/proof.
                                        servlet.SerializeEmployeesServlet";
        url = new URL( urlStr + "?");   
        }   catch( MalformedURLException e) {
            System.out.println(" URL: " + e.toString());
            e.printStackTrace();
            return null;
            }
    // Open the stream -- this starts the service() method in the servlet
    InputStream inStream = null;
    try{
        inStream = url.openStream();
    }   catch( IOException e){
        System.out.println("EmpolyeeApplet Exception: " + e.toString());
        e.printStackTrace();
        return null;
        }
    // Get Vector of Employees...
    Vector employees = null;    
    // Inflage Vector of Employee Objects
    employees = 
            (Vector) inflate( inStream);
        return employees;
}


Listing Five
/* Create a Vector of Employees, deflate and stream back to applet */   
public void service(HttpServletRequest req, HttpServletResponse res) 
                                      throws IOException, ServletException
{
    // Create the output stream for communicating with the Applet   
    FilterOutputStream objOut = 
                          new FilterOutputStream( res.getOutputStream());
    byte[] errorDeflatedExam = null;
    
    context = getServletContext();
    if (context != null) {
        context.log("LOG:ReqInfoServlet:service START");
    }   
    // Process arguments sent on the URL request
    processURLArguments(req);
    // Get a deflated Employee Object
    byte[] aDeflatedVector = (byte[]) buildDeflatedEmployees(employees);
    //      Write it to the applet      
    objOut.write(aDeflatedVector);
    objOut.close();

    context.log("LOG:EmployeeServlet:service  Employees written");
    context.log("LOG:ExamServlet:service END");
}


Listing Six
/* Turn Vector into a byte array.
 * @return java.io.ObjectOutputStream
 * @param anExam COM.cl.onlineExam.domain.Exam
 */
private byte[] buildDeflatedEmployees(Vector aVector) {
    //  Deflate the object to an output stream
    ObjectOutputStream objOut = null;
    ByteArrayOutputStream bOut = null;
    try {
        bOut = new ByteArrayOutputStream();
        objOut = new ObjectOutputStream(bOut);
        objOut.writeObject(employees);
        objOut.close();
    } catch (java.io.IOException e) {
        e.printStackTrace();
        return null;
        }       
    return bOut.toByteArray();
}

Listing Seven
/* Inflate input stream */  
Object inflate( InputStream in){
    Object obj = null;
    try{
        for( int i = 0; i < 100; i++){
            if( in.available() == 0)    {
                for( int j = 0; j < 10000; j++);
                continue;
            }   else {
                break;
            }
        }
        }   catch( IOException e){
            System.out.println("Error in debug code: " + e.getMessage());
        }
        try{
            ObjectInputStream objIn = new ObjectInputStream( in);
            obj = objIn.readObject();
        }   catch( IOException e){
            responseTextArea.setText("Error inflating 
                                     object: " + e + " " + e.getMessage());
        }   catch( ClassNotFoundException e){
            responseTextArea.setText("Error finding 
                                      class: " + e.getMessage());
        }
        return obj;
    }





4


