wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlet could not send Objects to an Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet could not send Objects to an Applet" Watch "Servlet could not send Objects to an Applet" New topic
Author

Servlet could not send Objects to an Applet

Richard Huges
Greenhorn

Joined: Nov 10, 2003
Posts: 6
Hi
I have an applet and a servlet. They use http and object-serialization. The communication between them works as long as I send objects only in one direction (either applet->servlet or servlet->applet). But when the servlet should return some objects straightly after receiving an object from the applet, the applet receives nothing.
Thanks

*********************************************************************************************
Applet Code (Method)
*********************************************************************************************
URL url = null;
URLConnection servletConnection = null;
Vector vStg = null;
try {
url = new URL("https://localhost:8080/servlet/vpl");
servletConnection = url.openConnection();
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
servletConnection.setRequestProperty("Content-type","application/x-java-serialized-object");
ObjectOutputStream outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
Msg outobj = new Msg(Msg.GET_STG);
outputToServlet.writeObject(outobj);
outputToServlet.flush();
outputToServlet.close();
ObjectInputStream inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
vStg = (Vector) inputFromServlet.readObject();
inputFromServlet.close();
}
catch(Exception g) {
g.printStackTrace();
}
return (vStg);
*********************************************************************************************
Servlet Code
*********************************************************************************************
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ObjectOutputStream outputToApplet = null;
ObjectInputStream inputFromApplet = null;
try {
response.setContentType("application/x-java-serialized-object");
inputFromApplet = new ObjectInputStream(request.getInputStream());
System.out.println(inputFromApplet);
test = (Msg) inputFromApplet.readObject();
System.out.println(test);
if(test.getAuswahl() == 2) /*Msg.GET_STG*/{
outputToApplet = new ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(mytestdat.vPlan);
outputToApplet.flush();
outputToApplet.close();
inputFromApplet.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
catch (java.lang.ClassNotFoundException g) {
g.printStackTrace();
}
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
Try it without closing the outputToServlet stream until after you read the input from servlet. I suspect that closing that stream closes the whole connection. Performing the flush() is enough to complete sending the object.
Bill
Richard Huges
Greenhorn

Joined: Nov 10, 2003
Posts: 6
I tried it, but there was no improvement
Richard Huges
Greenhorn

Joined: Nov 10, 2003
Posts: 6
I suppose, that I've tried now every sequence of
new ObjectOutputStream/-InputStream,
write/readObject, flush and close
I don't understand why it doesn't work
hopless Richard
 
GeeCON Prague 2014
 
subject: Servlet could not send Objects to an Applet