aspose file tools*
The moose likes Servlets and the fly likes Sending a XML file as a Servlet response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sending a XML file as a Servlet response" Watch "Sending a XML file as a Servlet response" New topic
Author

Sending a XML file as a Servlet response

saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Hi all,

I need to send a XML file from a servlet to another one as a servlet response or through Headers.

I found the few methods to achieve this
1) Sending the XML file object in the Servlet Output Stram
2) Opening the second servlet using URL connection and then add these details
3) Sending the file as an Http Header attachment.
In the second option I have a sample program like this

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class seXml extends HttpServlet //file://implements SingleThreadModel
{
private String asp_loc = "http://localhost:7001/ShowXml";
//private String xml = "<?xml version='1.0'?><vccc-points-redemption><vccc-cust><vccc-cust-oid-num>9697</vccc-cust-oid-num></vccc-cust></vccc-points-redemption>";//<?xml version='1.0'?><vccc-points-redemption><vccc-cust><vccc-cust-oid-num>9697</vccc-cust-oid-num></vccc-cust></vccc-points-redemption>
private String xml = "<catlog><book>Java</book></catlog>";
private String xmlStringValue="default";

protected void doGet ( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = new PrintWriter(res.getOutputStream() );
out.println("<html><body>Starting XmlTransfer....<br>");
out.flush();

try{
out.println("Attempting to connect to: " + asp_loc + "<br>");
java.net.URL asp_url = new java.net.URL(asp_loc);
java.net.URLConnection asp_con = asp_url.openConnection();
asp_con.setUseCaches(false);
asp_con.setDoOutput(true);
asp_con.setDoInput(true);
byte buf[] = xml.getBytes();
System.out.println("contents:"+buf);

out.println("XMLwritten: buffer.length= "+ buf.length +"<br>");
out.flush();
asp_con.setRequestProperty("Content-type","text/xml");//tect/xml
asp_con.setRequestProperty("Content-length",""+(buf.length));
System.out.println("going to create the Output Stream");
OutputStream data=asp_con.getOutputStream();
data.write(buf);
data.flush();
data.close();
System.out.println("created the Output Stream");
out.println("Reading Response...<br>");
out.flush();
System.out.println("going to create the Input Stream");
InputStream inp = asp_con.getInputStream();
System.out.println("created the Input Stream");
/*int c;
while((c=inp.read())!=-1)
{
System.out.println("reading the contents");
System.out.print((char)c);
}
System.out.println("file transfered");
out.print("File Transfered");*/


int len = asp_con.getContentLength();
System.out.println(len);
byte res_buf[] = new byte[len];
int nRead = 0;
while( nRead < len ) {
nRead += inp.read(res_buf, nRead, len-nRead);
System.out.println(" "+nRead);
}
String xmlResult = new String(res_buf, "utf-8");
inp.close();
out.println("xmlStringValue: '"+ xmlStringValue+"'<br></body></html>");
out.flush();
// System.out.println("Result:"+xmlResult);

/* ObjectInputStream in = new ObjectInputStream(asp_con.getInputStream());
out.println("Input Stream Created...<br>");
out.flush();
xmlStringValue = (String) in.readUTF();//was readObject
in.close();

out.println("xmlStringValue: '"+ xmlStringValue+"'<br></body></html>");
out.flush();*/

}
catch (Exception ex) {
out.println(ex.toString()+"<br>"+ex.getMessage()+"<br>"+xmlStringValue);
System.out.println(ex.toString()+"<br>"+ex.getMessage()+"<br>"+xmlStringValue);
out.flush();
}
}
}

I am not able to get the required output and I am getting 0 bytes when reading the input stream of the servlet2.

Please Help!

Thanks and regards,
Saravanan.K
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Hi Saravanan.

Welcome to JavaRanch. I wanted to let you know that we have a cool feature that would make your code more readable for posts.

Under the Add Reply button (when adding a post) there are 10 buttons, one of them is labeled "code" this will create CODE tags in your post, if you paste your code inside these tags, then the code in the thread will keep all your formatting, including indentations which make code easier to read.

There are also buttons for quoting, adding a link to a URL, a link to an EMAIL address, and a couple more cool ones.

Good Luck

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Saravanan,

I haven't read your code (due to formatting and length) and I don't understand why you need to pass the XML file between servlets but, if the servlets are part of the same app and/or session, why not just hold a reference to the object containing the XML file in the app or session context?

Jules
Matthew Amacker
Greenhorn

Joined: Aug 10, 2004
Posts: 5
Hello Saravanan,
I'm fairly new to this whole servlet thing myself but I've been able to get XML data back and forth between servlets on different machines. Without knowing what you are talking to, which could be a problem service in itself, it looks like you never call "connect" on your URLConnection.

Here is some code that seems to work for me, again, please take it with a grain of salt...



I use HTTPURLConnection which is a subclass of the one you use but should do what you want. After you have called connect you can then get your input and output streams.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending a XML file as a Servlet response