calling an .xml file which is accessable from the server but is not webserv
Hi,
I would like to call an .xml file which is accessable from the server but is not on the apache webserver. I would like to redirect the user to this .xml file but it not accessable to them since it does not reside within the apache "webapps" folder but instead is in another part of the server.
Thanks,
John J. Mitchell
# 1
You cannot directly access a plain file through the webserver however a Servlet or JSP can read it and send that as a reponse to you.
In the following case write a servlet which can read the xml file and give a text/xml reponse.
here is an example
public class XmlServlet extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
String fileName = new String("");
try{
// where filename gives us the real path of the file
fileName = request.getParameter("filename");
if(fileName == null)
fileName = "";
}catch(Exception exp){
}
boolean flag = new File(fileName).exists();
if(!fileName.equals("") && flag){
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
BufferedReader input = null;
try {
input = new BufferedReader( new FileReader(fileName) );
String line = null;
while (( line = input.readLine()) != null)
out.println(line);
}catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex){
ex.printStackTrace();
} finally {
try {
if (input!= null) {
input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
out.close();
} else{
response.setContentType("text/html");
response.getWriter().println("File Not Found");
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
}
and here is how you call it
XmlServlet?filename=C:\\xmlfiles\\Info.xml
Hope this helps :)
REGARDS,
RaHuL
# 2
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);
}
}
# 3
Hello,
I doubt whether redirection works as the RequestDispatcher always redirects to files in the web-application only.
However you can include XML files to which you can connect over the internet by opening a connection to them, reading them as InputStreams and copying them to the output:
URL url = new URL("http://www.myserver.com/dir/file.xml");
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
// now copy is to the response output
This works perfectly for pure XML files.
I would like to include XML files that contain custom tags though and am still figuring out how to do this or whether this is possible. See my post (under the servlets forum) if you could help me at:
http://forum.java.sun.com/thread.jspa?threadID=5175131
# 4
Thanks,
The below example worked except that embedded within each .xml file is a reference to a .dtd and .xsl file as an example:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE metadata SYSTEM "csdgm2.dtd">
<?xml-stylesheet href="FGDC_V2.xsl" type="text/xsl"?>
and since these lines are brought down to the clients machine it now considers the path to these files to start at the web application instead of where the .xml files are located which is where they actually reside.
How can I get this to work so that the server opens the .xml file and links to these .dtd and .xsl files and parsed before sending the rendered .xml file to the clients machine?
Thanks,
John