Possible errors when transforming XML in deployed Apache 6.0
I cannot display a transformed xml (transformed by xsls) when running a WAR file on a deployed Tomcat 6 server supplied to me, whereas when I run it on Tomcat which is on my machine it works fine.
Code:
StringReader xmlInput = new StringReader(xmlString);
InputStream xslInput = new URL(path).openStream();
Source xmlSource = new StreamSource(xmlInput);
Source xslSource = new StreamSource(xslInput);
// XML result will be written to HTTP response.
Result xmlResult = new StreamResult(response.getOutputStream());
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(xslSource);
transformer.trans开发者_如何学Goform(xmlSource, xmlResult);
} catch (Exception e) {
Log.err("xml xslt transform error");
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return ("return couldn't load file");
}
where path is a url of the xsl file on the deployed Tomcat server, which is available via GET request.
Why would this not work on the deployed machine?
I'm not sure exactly how the WAR file is run on the server, all I know is that it's run on an Tomcat 6.0 server and I cannot see the output of the server since my professor is not willing to share any of this information.
So, what I'm asking is what can cause this in general?
EDIT
I've managed to get some more information about the problem. first of all there is no error thrown, the servlet returns a 200 status. Second nothing is writen into the response, meaning that the transform function didn't do some part of it's job either the transforming or the writing to the response.
When dealing with opening resources from a Servlet
, it's generally a good idea to use the ServletContext
's GetResourceAsStream
, which will give you an InputStream
and get around that servlet resources may still be packed inside a WAR file.
Do note that the path that GetResourceAsStream
takes is relative to request.getContextPath()
.
You can get the current ServletContext
in a Servlet
via this.getServletContext()
If the server has a firewall, getting the xsl file may result in an error.
精彩评论