I use a byte array to figure out the content length:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeInt(pid);
out.flush();
byte[] buf = baos.toByteArray();
con.setRequestProperty("Content-type",
"application/octet-stream");
con.setRequestProperty("Content-length", "" + buf.length);
...
Then write the byte array to the output stream. It seems to work.
You may want to check out the book on Servlets by
Karl Moss, which explains in detail how this works.
I have heard of problems with serialization not working between
different versions of JVM, especially between 1.1 and 1.2.
Alex Amies
-----Original Message-----
From: Jim Tomlinson [SMTP:[EMAIL PROTECTED]]
Sent: Wednesday, September 22, 1999 3:50 PM
To: [EMAIL PROTECTED]
Subject: Weird JRun serialization problem
I have an applet that communicates with a GenericServlet via a serialized
object with code like this:
Foo protocolObject = new Foo();
URL mURL = new URL("http", "servletHost", 80,
"/servlet/FooServlet");
URLConnection mURLConnection;
if ((mURLConnection = mURL.openConnection()) != null)
{
// we'll be writing
mURLConnection.setDoOutput(true);
// and reading
mURLConnection.setDoInput(true);
// don't use any caching
mURLConnection.setUseCaches(false);
// no keep-alive
mURLConnection.setRequestProperty("Connection", "close");
// binary data
mURLConnection.setRequestProperty("Content-Type",
"application/octet-stream");
// get its output connection
ObjectOutputStream mOOStream = new
ObjectOutputStream(mURLConnection.getOutputStream());
// communicate a Foo to the servlet
mOOStream.writeObject(protocolObject);
mOOStream.flush();
mOOStream.close();
// get response back in the form of another Foo
ObjectInputStream mOIStream =
new ObjectInputStream(mURLConnection.getInputStream());
Foo response = ((Foo) mOIStream.readObject());
mOIStream.close();
}
So this code works swimmingly with ServletExec, JavaWebServer, WebSphere,
and JRun 2.2, but with JRun 2.3, there's a
java.io.StreamCorruptedException ("Caught EOFException while reading the
stream header")
thrown on the readObject(). I see the same exception being logged by the
servlet when it's reading the object the applet writes in the code above.
Has anyone seen anything like this, or have any suggestions on how I could
make JRun 2.3 happy? Thanks in advance for any and all pointers.
---
Jim Tomlinson [EMAIL PROTECTED] 206.217.7927
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html