GeeCON Prague 2014*
The moose likes Servlets and the fly likes Submit xml data to an Url from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Submit xml data to an Url from a servlet" Watch "Submit xml data to an Url from a servlet" New topic
Author

Submit xml data to an Url from a servlet

Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
How can i submit xml data to an Url from a servlet?
XML data is created on a string buffer and now i want to submit it to a url. This should be done with out using attributes..............ie should be writen to the stream.
Please help me out..........

Thanks in advance.


SCJP 1.4, Brainbench
LinkedIn - Blog
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Strictly speaking this is an IO question and has nothing to do with Servlets. It doesn't matter whether you do this from a Servlet or any other class. you can use the java.net package or a stronger version in the Jakarta HttpComponents.

Dave
Chris Grimble
Greenhorn

Joined: Jan 19, 2005
Posts: 3
I've used a bit of code like this in the past.

private void sendXml(String xml, URL endPoint) {
try {
OutputStreamWriter out = null;
try {
URLConnection connection = endPoint.openConnection();
connection.setDoOutput(true);
out = new OutputStreamWriter(connection.getOutputStream());
out.write(xml);
out.flush();
} finally {
if (out != null) {
out.close();
}
}
} catch (IOException e) {
//Handle exception
}
}
Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
Thanks Chris. But how can i read the response back from the server. i tried input stream but it is returning internal server error.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

You may want to look into this article that mentions the numerous URLConnection pitfalls and how to avoid them.

ram.
 
GeeCON Prague 2014
 
subject: Submit xml data to an Url from a servlet