java struts2 xml
hi mates,
Plese give me some clues for some of my questions.
I stored xml file data as xmlType in oracle database.
I am also using struts 2 for the project.
I wanna retrieve that whole xml file in the form of string and send them to the client html table. How can I catch that whole string xml file from client side with or without using the strut 2 features, objects are on the memory.
Second question is , how could I send the xml file data in string format back to server? I do appreciate for ur help. cheers.
[551 byte] By [
tonetonea] at [2007-11-27 8:38:10]

# 1
use a simple servlet to send xml data.
example 1
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendXml extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//get the 'file' parameter
String fileName = (String) request.getParameter("file");
if (fileName == null || fileName.equals(""))
throw new ServletException(
"Invalid or non-existent file parameter in SendXml servlet.");
// add the .doc suffix if it doesn't already exist
if (fileName.indexOf(".xml") == -1)
fileName = fileName + ".xml";
String xmlDir = getServletContext().getInitParameter("xml-dir");
if (xmlDir == null || xmlDir.equals(""))
throw new ServletException(
"Invalid or non-existent xmlDir context-param.");
ServletOutputStream stream = null;
BufferedInputStream buf = null;
try {
stream = response.getOutputStream();
File xml = new File(xmlDir + "/" + fileName);
response.setContentType("text/xml");
response.addHeader("Content-Disposition", "attachment; filename="
+ fileName);
response.setContentLength((int) xml.length());
FileInputStream input = new FileInputStream(xml);
buf = new BufferedInputStream(input);
int readBytes = 0;
while ((readBytes = buf.read()) != -1)
stream.write(readBytes);
} catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
} finally {
if (stream != null)
stream.close();
if (buf != null)
buf.close();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
example 2
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResourceServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//get web.xml for display by a servlet
String file = "/WEB-INF/web.xml";
URL url = null;
URLConnection urlConn = null;
PrintWriter out = null;
BufferedInputStream buf = null;
try {
out = response.getWriter();
url = getServletContext().getResource(file);
//set response header
response.setContentType("text/xml");
urlConn = url.openConnection();
//establish connection with URL presenting web.xml
urlConn.connect();
buf = new BufferedInputStream(urlConn.getInputStream());
int readBytes = 0;
while ((readBytes = buf.read()) != -1)
out.write(readBytes);
} catch (MalformedURLException mue) {
throw new ServletException(mue.getMessage());
} catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
} finally {
if (out != null)
out.close();
if (buf != null)
buf.close();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Hope That Helps