Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

843841Dec 15 2003 — edited Nov 6 2006
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-serialized-object");
          servletConnection.setRequestMethod("POST");     
          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
...

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 4 2006
Added on Dec 15 2003
10 comments
1,113 views