Wendy;

Not really related to your problem, but make sure you
close your ResultSet and Statement when done -- otherwise
weird things can occur (especially with connection
pools.)

Related to your problem:
Don't you have to do a "con.showDocument(url)"
somewhere?

-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of Wendy
Fry
Sent: Friday, January 28, 2000 5:18 AM
To: [EMAIL PROTECTED]
Subject: Applet to Servlet communication


Please help!

I've read all of the messages and been to all of the recommended websites
regarding Applet to Servlet communication with serializable objects but
I'm still having problems with the following code.  It seems as though the
applet successfully connects to the servlet, but the servlet never runs.

I had a simple servlet that connected to the database, made updates, and
that was all, so I know that part should work.  But in this example, the
database is never updated.

The following is produced in the Java Console when I run the applet:


Opening connection...done.
Request Property set to: application/octet-stream
Getting ObjectOutputStream...done.
Writing wsr object...done.
Getting ObjectInputStream...IOException: The document contained no data.
Try again later, or contact the server's administrator.


Thank you for any help anyone can offer.

Sincerely,

Wendy Fry


Here is the applet code:

*****

import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;

public class TestApp extends Applet
{
  private URLConnection con = null;
  private Vector v = null;

  public void start()
  {
    // Instance of WSRecord class
    WSRecord wsr = new WSRecord("John Doe", "[EMAIL PROTECTED]", "123-4567", ...);

    // Open connection to servlet
    try
    {
      URL url = new URL(getCodeBase(), "/servlets/dbserver");
        System.err.print("Opening connection...");
      con = url.openConnection();
        System.err.println("done.");

      con.setDoOutput(true);
      con.setDoInput(true);
      con.setUseCaches(false);
      con.setDefaultUseCaches(false);
      con.setRequestProperty("Content-Type", "application/octet-stream");
        System.err.println("Request Property set to: " +
con.getRequestProperty("Content-Type"));

      // Open output stream and send wsr object
        System.err.print("Getting ObjectOutputStream...");
      ObjectOutputStream out = new
ObjectOutputStream(con.getOutputStream());
        System.err.println("done.");
        System.err.print("Writing wsr object...");
      out.writeObject(wsr);
        System.err.println("done.");
      out.flush();
      out.close();

      // Open input stream and receive vector object from servlet
      // Vector v contains many WSRecord objects
        System.err.print("Getting ObjectInputStream...");
      ObjectInputStream objin = new ObjectInputStream(con.getInputStream());
        System.err.println("done.");
        System.err.println("Waiting for response...");
      v = (Vector)objin.readObject();
        System.err.println("done.");
      objin.close();
    }
    catch(MalformedURLException murle)
    {
      System.err.print("MalformedURLException: " + murle.getMessage());
    }
    catch(ClassNotFoundException cnfe)
    {
      System.err.println("ClassNotFoundException: " + cnfe.getMessage());
    }
    catch(IOException ioe)
    {
      System.err.println("IOException: " + ioe.getMessage());
    }
  }
}

*****

Here is the servlet code:

*****

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

public class dbserver extends HttpServlet
{
  private Connection con = null;

  protected String url = "jdbc:postgresql:customdb";
  protected String driver = "postgresql.Driver";
  protected Statement stmt = null;
  protected Vector wsrvector = null;

  public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
  {
    try
    {
      ObjectInputStream objin = new
ObjectInputStream(request.getInputStream());
      WSRecord wsrin = (WSRecord)objin.readObject();
      objin.close();

      Class.forName(driver);
      con = DriverManager.getConnection(url, "user", "password");

      stmt = con.createStatement();

      stmt.executeUpdate("insert into INFO values(wsrin.get_poc(),
wsrin.get_email(), wsrin.get_phone, wsrin.get_title,
wsrin.get_description())");

      String query = "select * from INFO, DATES where (INFO.key  =
DATES.key)";
      ResultSet rs = stmt.executeQuery(query);
      ResultSetMetaData rsmd = rs.getMetaData();
      int numColumns = rsmd.getColumnCount();

      stmt.close();

      wsrvector = new Vector();

      while (rs.next())
      {
        WSRecord wsr = new WSRecord();

        wsr.set_poc((String)rs.getObject(1));
        wsr.set_email((String)rs.getObject(2));
        wsr.set_phone((String)rs.getObject(3));
        wsr.set_title((String)rs.getObject(4));
        wsr.set_description((String)rs.getObject(5));

        .....

        wsrvector.addElement((Object)wsr);
      }

      response.setContentType("application/octet-stream");
      ObjectOutputStream objout = new
ObjectOutputStream(response.getOutputStream());
      objout.writeObject(wsrvector);
      objout.flush();
      objout.close();
    }
    catch(java.lang.ClassNotFoundException e) { }
    catch(SQLException sqle) { }
    catch(Exception e) { }

    finally
    {
      try
      {
        if (con != null)
        {
          con.close();
        }
      }
      catch(SQLException ignored) { }
    }
  }

  public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
  {
    doPost(request, response);
  }
}

*****

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to