Problem reading XML doc

In my servlet, I create a org.jdom.Document and I send it in my doGet(HttpServletRequest request, HttpServletResponse response) as the following:

response.getWriter().print(document);

Then in my HttpUnit I do the following:
WebResponse response = client.getResponse("http://localhost:8080/myServlet");
ObjectInputStream input = new ObjectInputStream(response.getInputStream());
Document document = (Document) input.readObject();
XMLSerializer.printXmlDoc(document, System.out, true);

The test is failing in the second line above with a StreamCorruptedException.

Any help would be greatly appreciated.
salmasriAsked:
Who is Participating?
 
objectsCommented:
Thats not sending a serialized Document , should be sent more like:

ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
out.writeObject(document);
0
 
CEHJCommented:
Yes, don't use ObjectInputSTream
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Writers and readers are used for character and text-based data. In your case since you are not dealing with that, you need to directly deal with the serialized form of the object.
0
 
CEHJCommented:
<"virtually deleted comment", reinstated by GranMod>

Server:

response.setContentType("text/xml");
new XMLOutputter().output(document, response.getWriter());

...

Client:

WebResponse response = client.getResponse("http://localhost:8080/myServlet");
Document document = new SAXBuilder().build(response.getInputStream());
new XMLOutputter().output(document, System.out);

Your client should just as easily be able to be your web browser
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I remember one of the comments posted here later by CEHJ was a useful approach too (the one about using XML).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.