File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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 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 {
PrintWriter out = new PrintWriter(res.getOutputStream() );
out.println("<html><body>Starting XmlTransfer....<br>");

out.println("Attempting to connect to: " + asp_loc + "<br>"); asp_url = new; asp_con = asp_url.openConnection();
byte buf[] = xml.getBytes();

out.println("XMLwritten: buffer.length= "+ buf.length +"<br>");
System.out.println("going to create the Output Stream");
OutputStream data=asp_con.getOutputStream();
System.out.println("created the Output Stream");
out.println("Reading Response...<br>");
System.out.println("going to create the Input Stream");
InputStream inp = asp_con.getInputStream();
System.out.println("created the Input Stream");
/*int c;
System.out.println("reading the contents");
System.out.println("file transfered");
out.print("File Transfered");*/

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

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

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

catch (Exception ex) {

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,
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

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


Perfect World Programming, LLC - iOS Apps
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?

Matthew Amacker

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:
subject: Sending a XML file as a Servlet response
It's not a secret anymore!