Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Tomcat Users »

Problems with sending a serialized object from servlet to applet
 using Tomcat

Darina Dicheva.

2003-12-15


Hello,

I have a problem sending a serialized object (instance of a class that is
not part of the Java Core API) from a servlet to an applet. I don't have a
problem to send a serialized String object though - everything works fine
(even in both directions). But when I replace the String object to be sent
by the servlet to the applet with an object of a class defined by me,
'inputFromServlet.readObject()' is just hanging -- no exception, no error,
nothing. The servlet itself seems to have finished its work completely. I
have copied the class definition (of the object being serialized) in the
applet's directory.

I use Tomcat 4.1.29 and j2sdk1.4.2.

Any idea will be highly appreciated. Thanks in advance.

Darina

----------------------
In the applet I have:
try {
      URL servletURL = new URL(getCodeBase(), servletLocation2);
      HttpURLConnection servletConnection = (HttpURLConnection)
servletURL.openConnection();
      servletConnection.setDoOutput(true);  
      servletConnection.setDoInput(true);  
      servletConnection.setUseCaches(false);
      servletConnection.setDefaultUseCaches(false);

servletConnection.setRequestProperty("Content-type","application/x-java-seri
alized-object");
      servletConnection.setRequestMethod("POST");   //if not included
the defaul is the GET method

      log("Applet Connected");

      // Writing to servlet

      // Write the message to the servlet
      OutputStream os = servletConnection.getOutputStream(); // returns
an output stream that writes to this connection
      ObjectOutputStream outputToServlet = new ObjectOutputStream(os);


      // serialize the object
    outputToServlet.writeObject("Message To Server");
     
    outputToServlet.flush();      
    outputToServlet.close();
    log("Writing Complete.");  

      // Reading from servlet

      InputStream is = servletConnection.getInputStream();
      ObjectInputStream inputFromServlet = new ObjectInputStream(is);
      log("Object Input stream created");

      Object obj = inputFromServlet.readObject();

// HANGS UP HERE !!

      Topic response = (Topic)obj;
      log("Finish reading data");
      inputFromServlet.close();
  }
  catch ...
----------------------

In the servlet I have:

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

  try
  {
     InputStream is = request.getInputStream();  //get an input stream
that reads from from this open connection
     ObjectInputStream inputFromApplet = new ObjectInputStream(is);
     show("Servlet Connected");

   String appStr = (String) inputFromApplet.readObject();
   show("Applet String: " + appStr);    
   inputFromApplet.close();            

    // Ctreate a topic
     Topic topic = new Topic("Number1 Systems", "tt-NumberSystem1",
"N1", "Number1 System", "N1");
     show(topic.toString());

     response.setContentType("application/x-java-serialized-object");

     OutputStream os = response.getOutputStream(); // returns an output
stream that writes to this connection
     ObjectOutputStream outputToApplet = new ObjectOutputStream(os);
     show("Servlet connected");

     outputToApplet.writeObject(topic);
     outputToApplet.flush();      
     outputToApplet.close();
     show("Data transmission complete.");

  }
  catch ...

Can somebody help??? I am struggling with this problem for more than a week
...

©2008 junlu.com - Jax Systems, LLC, U.S.A.