• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Sending a XML file as a Servlet response

Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 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 {
PrintWriter out = new PrintWriter(res.getOutputStream() );
out.println("<html><body>Starting XmlTransfer....<br>");

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();
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 += inp.read(res_buf, 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,
Posts: 17347
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic