aspose file tools*
The moose likes Servlets and the fly likes Sending XML over HTTP to Another Application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sending XML over HTTP to Another Application" Watch "Sending XML over HTTP to Another Application" New topic
Author

Sending XML over HTTP to Another Application

Ajoo Dar
Greenhorn

Joined: Dec 14, 2009
Posts: 5
Hi,
I have 2 web application ClientA and ServerB. On ClientA application My requirement is I need to read an XML from a file system and then send it to
ServerB. This XML needs to be sent to ServerB over HTTP or HTTPs. HTTP is enough right now. When ServerB receives the XML, it will
do some basic validation, say for example one of the validation is if it is well formed. If everything is fine it will send response back to
ClientA

Input Message:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Send me Amount details</message>
</note>

Syschronous Response:

<?xml version="1.0" encoding="ISO-8859-1"?>
<response>
<Status code="201" text="Accepted">Acknowledged</Status>
</response>


Requirement 2:


On the server side I need to create a thread or a process which will read say n number of XMLs and send asychronously to ClientA.

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<from>Tove</from>
<to>Jani</to>
<amount>29</amount>
</note>

How can it be done? Any thought on this.... I want to use Java/Servlets... or if we can use webservices or JMS. I want to do it using Java technology
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Ajoo Dar wrote:How can it be done? Any thought on this.... I want to use Java/Servlets... or if we can use webservices or JMS. I want to do it using Java technology


Is this a homework assignment?

Yes it can be done. I'd do it on the server side with a servlet.

Its unclear what your requirement is on the client, one could present a form with a big textfield, have the user cut and paste into the field, and then do a POST of the form to the servlet. Or write a client-side application in java, etc.
Ajoo Dar
Greenhorn

Joined: Dec 14, 2009
Posts: 5
Thanks for your response, Yes it is homework example

Here is what I need to do on client side, my file is stored into my local drive....I need to read the XML and then send it to ServerB. After doing some validation on ServerB an XML message is sent back to Client.. It will be an Synchronous. Right now validation on server side will be to check if the xml file is well formed..

Synchronous Response:

<?xml version="1.0" encoding="ISO-8859-1"?>
<response>
<Status code="201" text="Accepted">Acknowledged</Status>
</response>

--

Thanks
Ajoo
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
The java.net package has various classes that implement HTTP connectivity: see examples e132 to e139 in http://www.exampledepot.com/egs/java.net/pkg.html


Ping & DNS - my free Android networking tools app
Ajoo Dar
Greenhorn

Joined: Dec 14, 2009
Posts: 5
Ok, Here is what I have done so far..
How will I process from here to send the Document doc to another server
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Inside CSMServlet");
try{
File file = new File("c:\\Java_J2ee\\MyXMLFile.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
System.out.println("******Document is***** "+doc);
doc.getDocumentElement().normalize();

System.out.println("Root Element is "+ doc.getDocumentElement().getNodeName());

//Here I need to send XML to another application


/*
NodeList nodeList = doc.getElementsByTagName("info");

System.out.println("Node List is "+nodeList.getLength());

for(int s= 0; s<nodeList.getLength(); s++){

Node fstNode = nodeList.item(s);
if(fstNode.getNodeType() == Node.ELEMENT_NODE){

Element msgElmnt = (Element) fstNode;
NodeList msgElmntLst = msgElmnt.getElementsByTagName("message");
Element msgElmnt2 = (Element) msgElmntLst.item(0);
NodeList msg = msgElmnt2.getChildNodes();
System.out.println("Message from the element is "+ msg.item(0).getNodeValue());

}

}
*/



} catch(Exception e){

e.printStackTrace();
}




}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
You don't need to create a Document object; just read the bytes that make up the XML file (using FileInputStream) and send them in the POST body. Make sure to set the content encoding accordingly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending XML over HTTP to Another Application