This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending a XML file as a Servlet response

saravanan kanda swamy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic