Hi all,
I am in the process of trying my hand at using XSL/T to convert XML output
from a JSP page, into HTML. The code I have thus far uses the latest XALAN
processor (which comes with the latest XERCES parser). Thus, I am using the
TrAX APIs. Here is my code snippet:
// for the sake of bevity, r is a Resource object (a class I created) that
contains mappings from an xml config file. Mostly, the getName() returns a
path to a JSP page relative to the web-app (in other wrods r.getName() =
"/inside/page.jsp" not "c:\dir\www\inside\page.jsp"). getXSL() returns the
relative web-app path to an XSL that is to be used to transform the XML
output the JSP page returns, to HTML.
// Transform the xml output using the xsl stylesheet and the XALAN XSLT
// engine, placing the output stream of the transformation in the
// HttpServletResponse output stream.
response.setContentType("text/html"); // always assume HTML output from
this controller
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(new
FileInputStream(getServletContext().getRealPath(r.getXSL()))));
transformer.transform(new StreamSource(new
FileInputStream(getServletContext().getRealPath(r.getName()))), new
StreamResult(response.getOutputStream()));
}
catch(Exception e)
{
System.out.println("Exception while trying to transform document.
MESSAGE = " + e.getMessage());
// attempt to forward to an error page
try
{
getServletContext().getRequestDispatcher("/no_mapping.jsp").forward(request,
response);
}
catch(Exception e2)
{
System.out.println("Exception trying to forward to /no_mapping.jsp.
MESSAGE = " + e2.getMessage());
return;
}
}
I am not sure why, but this doesn't seem to work. I originally got a "file
not found 'file:///xsl/demo.xsl'" error and then I added the
getServletContext().getRealPath(r.getXSL()) so that it would hopefully
return a path relative to the web-app the page is found in.
The message I get now is TrAX Tranform Exception..or something like that
(Sorry..I am at work and I don't have the web-app here to test and get the
exact message).
My main interest is in supplying the XSL and the XML to the XALAN processor,
which then fills the response.outputstream with the result..HTML output, and
sends that back. I would like it as streamlined as possible..for speed. I
have yet to examine the output of the JSP page..so I am not sure that it is
doing what it should.
Where I am really stuck is in "getting" the JSP page and the XSL page.
Ideally, I need to access the JSP page as if a web browser is hitting it,
and grab its output..this is the only way to invoke the JSP processor to
compile the JSP into a servlet (if not already done), and treat it as if a
web browser was accessing it. I want to use JSP so that I can dynamcially
put data from a javabean in the output. Otherwise..I would just use XML. So,
the two areas I am most in need of help:
1) How do I use URL and/or URLConnection to make a URL request to a JSP page
"relative" to the web-app the path is in.
2) How do I read in the XSL via an InputStream so that I have the exact file
itself?
Thanks for any help.
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets