Sending a serialized object by http post parameter

J

John

Hi all,

I just working on the following problem: I'd like to send a serialized
object from one web application to a second one by using a hyperlink.
(by the way: both are using the struts framework). The hyperlink now
should get the serialized object as a url parameter.

Im not quite firm with object serialization. For this, I have read some
topics found in several groups and I have used the following coding
found:

My hashtable is the object I'd like to send as a serialized object by
using the http post and the object should be passed as URL parameter.

public String test() {
Hashtable data = new Hashtable();
data.put("first","test value");

java.io.ByteArrayOutputStream ostream = new
ByteArrayOutputStream();
try {
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeObject(data);
p.flush();
return p.toString();

} catch (IOException e) {
log.error(e);
return null;
}

}

Does anybody has some experiences with serializing objects and sending
them as an URL parameter?

Thanks in advance
John
 
D

Daniel Hagen

John said:
Hi all,

I just working on the following problem: I'd like to send a serialized
object from one web application to a second one by using a hyperlink.
(by the way: both are using the struts framework). The hyperlink now
should get the serialized object as a url parameter.

Im not quite firm with object serialization. For this, I have read some
topics found in several groups and I have used the following coding
found:

My hashtable is the object I'd like to send as a serialized object by
using the http post and the object should be passed as URL parameter.

public String test() {
Hashtable data = new Hashtable();
data.put("first","test value");

java.io.ByteArrayOutputStream ostream = new
ByteArrayOutputStream();
try {
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeObject(data);
p.flush();
return p.toString();

You should be careful here: ByteArrayOutputStream uses the platform's
default encoding so you could get in trouble if the default encodings on
source and target system are different.
Anyway I would suggest a more direct approach (see below).
} catch (IOException e) {
log.error(e);
return null;
}

}

Does anybody has some experiences with serializing objects and sending
them as an URL parameter?


Why pass the serialized URL object as URL Parameter?
I would suggest URL-encoding the the result of URL.toString() and then
using that String directly, or writing the URL as second object to your
ObjectOutputStream, so either

void send(Hashtable data, URL parameter)
{
URL targetUrl = new URL("http://somewhere/someservlet?url=" +
java.net.URLEncoder.encode(parameter, SOME_ENCODING ));
URLConnection connection = targetUrl.openConnection();
connection.setDoOutput( true );
OutputStream urlOutputStream = connection.getOutputStream();
ObjectOutputStream objectOutput = new ObjectOutputStream(urlOutputStream);
objectOutput.writeObject( data );
// [flush, get any response, close etc.]
}


void send(Hashtable data, URL parameter)
{
URL targetUrl = new URL("http://somewhere/someservlet");
URLConnection connection = targetUrl.openConnection();
connection.setDoOutput( true );
OutputStream urlOutputStream = connection.getOutputStream();
ObjectOutputStream objectOutput = new ObjectOutputStream(urlOutputStream);
objectOutput.writeObject( parameter );
objectOutput.writeObject( data );
// [flush, get any responser, close etc.]
}


On the receiving side you would read directly from
request.getInputStream(), eg
ObjectInputStream o = new ObjectInputStream(request.getInputStream());

(All code typed out of my head, so don't rely on completeness or
correctness)

You should be aware of the problems coming along with object
serialization such as changing of classes problems communicating between
different JRE Versions etc (you should find plenty of information about
that on the SUN Website or on the usenet).

Hope that helps.

Daniel
 
A

Alex

Does anybody has some experiences with serializing objects and
sending them as an URL parameter?
I agree that it looks cool. But I don't agree that this is good idea.
Even it will work.
Hint: do you know that serialized object/stream could have
noneprintable characters?
Next hint: do you know what %nn means in url?
Next hont: do you remember that such conversation should go both ways
(serialization/deserialization).

Alex Kizub.
 
W

Wendy S

John said:
My hashtable is the object I'd like to send as a serialized object by
using the http post and the object should be passed as URL parameter.

On commons-user (Jakarta Commons mailing list) someone is trying to do this,
and his method is to serialize the object and then base-64 encode the binary
data so it will be a String.
 
R

Remi Bastide

You could also consider using XMLEncoder as an alternative to
Serialization. It produces an XML representation of (almost) any
object, that can be sent safely.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top