Problem with object serialization in Applet-Servlet communication
Posted
by Bruce
on Stack Overflow
See other posts from Stack Overflow
or by Bruce
Published on 2010-05-02T12:02:49Z
Indexed on
2010/05/02
12:07 UTC
Read the original article
Hit count: 277
applet
|communication
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)
{
// ...
}
© Stack Overflow or respective owner