Problem with object serialization in Applet-Servlet communication
- by Bruce
Hi guys,
I spent a lot of time on thinking what is wrong with the following code. I
send the object from my applet to servlet and then I read the object from servlet.
Everything goes fine till reading serialized object from the servlet -
I got IOException.
Thank you in advance!
Here is the code:
Applet:
try {
URL servletURL = new URL(this.getCodeBase().getProtocol(), this.getCodeBase().getHost(), this.getCodeBase().getPort(),
"/MyApplet");
URLConnection servletConnection = servletURL.openConnection();
servletConnection.setDoInput( true );
servletConnection.setDoOutput( true );
servletConnection.setUseCaches( false );
servletConnection.setRequestProperty( "Content-Type", "application/x-java-serialized-object" );
ObjectOutputStream output;
output = new ObjectOutputStream( servletConnection.getOutputStream( ) );
output.writeObject( someObject );
output.flush( );
output.close( );
ObjectInputStream input = new ObjectInputStream( servletConnection.getInputStream( ) ); // Here I got the exception
myObject = ( SomeObject ) input.readObject( );
} catch (java.io.IOException ioe) {
System.err.println(ioe.getStackTrace());
} catch (Exception e) {
System.err.println(e.getStackTrace());
}
Servlet:
response.setContentType("application/x-java-serialized-object");
try {
ObjectInputStream inputFromApplet = new ObjectInputStream(request.getInputStream());
SomeObject myObject = (SomeObject) inputFromApplet.readObject();
ObjectOutputStream outputToApplet = new ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(myObject);
outputToApplet.flush();
}
catch(Exception e)
{
// ...
}