• 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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 {
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
 
ranger
Posts: 17346
11
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

Mark
 
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?

Jules
 
Greenhorn
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.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic