April 5, 2011, 9:24 a.m.
posted by randomuser
If a servlet engine such as Tomcat or Jetty is already running on the web server, the simplest way to add XSLT support is by using one of the prewritten servlets. The downside to this is that you can't always easily map existing URLs and documents into such a system, but this approach does let you put up new content in new directories very quickly.
Michael Kay's Saxon includes a simple servlet in its samples directory that performs basic transformations. This isn't really intended as a full-blown, server-side transformation engine—just a little code to show you how to roll your own. Still it's impressively small and should work well with any servlet container such as Tomcat or JRun. Once you've installed this servlet (or a customized version of it) on your server, you'll be able to load XML documents through URLs that look like this:
The source parameter is a URL pointing to the XML document. The style parameter is a URL pointing to the stylesheet to use for the transformation. Both are relative to the location of the servlet context. However, with a little custom coding, it's easy to select a different base directory for either or both.
Internet Explorer has a nasty habit of relying on the last few letters of the URL to determine the MIME type. When IE sees a URL ending in .xml or .xsl, even in the query string, it treats the result as XML, regardless of what the MIME type actually is. You can add an unused parameter to the query string to avoid this, as demonstrated below.
When the user requests such a URL, the servlet calls TrAX to transform the input document according to the instructions in the stylesheet and then sends the result to the client browser. The output document is streamed to the client as quickly as possible. The entire transformation does not need to be complete before the first byte of data is sent (though XSLT is really not designed for streaming, and generally quite a bit of work will need to be done before the first byte is sent, just not all of the work).
This servlet is half caching: It holds onto the compiled stylesheet so it doesn't have to reparse it every time it's used. Furthermore, it's thread safe so the same servlet and stylesheet can be used for multiple requests simultaneously. However, it does not store the transformed document. Thus a file must be read from disk and the transformation applied every time the document is requested. Since most web servers do not cache documents loaded with query strings, this can be a performance issue on a high-volume server. If you use this servlet for important work, one of the first things you should probably add is an output cache that holds the transformed document. If the server has a lot of RAM and serves fairly small documents, you may want to store them in memory. Otherwise, you can save them in a temporary directory somewhere on the disk, perhaps saving only the most frequently requested files.
Xalan-J includes several sample servlets that can process XSLT on the server side. These include a servlet that reads an xml-stylesheet processing instruction in the XML document to figure out which stylesheet to apply and a servlet that uses the CGI query string to set XSLT parameters. (See Item 39.) Xalan also provides a Java Server Page implementation. However, the Xalan samples don't do any caching, so they're even less suited to production use than the Saxon servlet.
In fact, since all these servlets are based on the generic TrAX API rather than on the specifics of Xalan or Saxon, you can use them with either processor or others. Since you'll normally want to customize the servlets anyway, you'll probably want to borrow code from all of them when it seems useful to you.