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/17 00:51:00 UTC

RE: Problems with sending a serialized object from servlet to app let using Tomcat

Thanks to all that helped me with suggestions - I solved my problem.
Darina

> -----Original Message-----
> From: Dicheva, Darina [mailto:dichevad@wssu.edu]
> Sent: Monday, December 15, 2003 11:17 AM
> To: 'tomcat-user@jakarta.apache.org'
> Subject: Problems with sending a serialized object from servlet to
> applet using Tomcat
> 
> 
> 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","applicati
> on/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
> ...
>  
>