We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Problem reading XML doc

salmasri
salmasri asked
on
Medium Priority
245 Views
Last Modified: 2012-05-05
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.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Yes, don't use ObjectInputSTream
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Thats not sending a serialized Document , should be sent more like:

ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
out.writeObject(document);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
<"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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
I remember one of the comments posted here later by CEHJ was a useful approach too (the one about using XML).
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.