You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Dicheva, Darina" <di...@wssu.edu> on 2003/12/13 20:40:11 UTC

Sending a serialized object to/from servet

Hello,

I have a problem sending a serialized object in both directions
applet2servlet and servlet2applet. I use Tomcat 4.1.29 and j2sdk1.4.2.

In the applet I have:
try  {  
   
        // Create an object to communicate with the servlet
        URL servletURL = new URL(servletLocation2);
        HttpURLConnection servletConnection =
(HttpURLConnection)servletURL.openConnection();

        servletConnection.setDoInput(true);     
        servletConnection.setDoOutput(true);    
        servletConnection.setUseCaches(false);
        servletConnection.setDefaultUseCaches(false);
 
servletConnection.setRequestProperty("Content-type","application/x-java-seri
alized-object");
        servletConnection.setRequestMethod("POST");

        log("Applet Connected.");

        // Writing to servlet
        ObjectOutputStream outputToServlet = new
ObjectOutputStream(servletConnection.getOutputStream());

        log("Got ObjectOutputStream object.");
	  outputToServlet.writeObject(topic);
	        
	  outputToServlet.flush();	        
	  outputToServlet.close();
	  log("Writing Complete.");        

        // Reading from servlet
        ObjectInputStream inputFromServlet = new
ObjectInputStream(servletConnection.getInputStream());
        returnedTopic = (Topic) inputFromServlet.readObject();
        inputFromServlet.close();
        log("Reading Complete.");        
     }
     catch ...
-----

In servlet I have:

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

      Topic topic, aTopic;
      try
        {
          System.out.println("Servlet Started");
          // get an input stream from the applet and create an object input
stream
          ObjectInputStream inputFromApplet = new
ObjectInputStream(request.getInputStream());
	    topic = (Topic) inputFromApplet.readObject();
          inputFromApplet.close();                
 
         // servlet processes the topic here
         // Ctreate a topic
         aTopic = new Topic("Number1 Systems", "tt-NumberSystem1", "N1",
"Number1 System", "N1");
         response.setContentType("application/x-java-serialized-object");
         ObjectOutputStream outputToApplet = new
ObjectOutputStream(response.getOutputStream());
         outputToApplet.writeObject(aTopic);
         System.out.println ("Data transmission complete.");
 
         outputToApplet.flush();          
         outputToApplet.close();
        }
        catch  ....

When running the applet, I receive the message  "Got ObjectOutputStream
object.", but not "Writing Complete." Moreover, the servlet doesn't print
the "Servlet Started" message.

If I comment out the part related to writing a serialized object in the
applet and leave just the part for reading from servlet, the servelt  seems
to work properly printing the messages "Servlet Started" and "Data
transmission complete.", but the applet again doesn't go after the
readObject call (doesn't print "Reading Complete").

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

Darina