2

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.transform(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.

Whitebear
  • 1,761
  • 2
  • 12
  • 22
  • Where does the `xmlString` come from? Is that different between your deployed/local machines? – Mads Hansen Aug 04 '11 at 02:04
  • Are you sure that it produces nothing, or that you just can't see the results(try view-source). If you transform into XML output, it may not display well in the browser(which may only render `text()` nodes and seem as if there was no output). – Mads Hansen Aug 04 '11 at 02:07
  • I construct xmlString by myself. it is definitly produced correctly on both my machine and there machine, It produces what it's supposed to produce when the transform is run on my local machine, thing is it doesn't return anything (once again i've checked) on the deployed machine. – Whitebear Aug 04 '11 at 08:13

2 Answers2

0

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()

Powerlord
  • 87,612
  • 17
  • 125
  • 175
0

If the server has a firewall, getting the xsl file may result in an error.

Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98