Saxon servlet

The sample application SaxonServlet.java is a general-purpose servlet that takes the name of a source document and XSLT stylesheet as URL parameters, and uses the stylesheet to process the source document, creating a result document which is sent back to the browser for display. The result document may have any media type, though HTML and XML are the most likely.

The servlet maintains a cache of prepared stylesheets; if the same stylesheet is used repeatedly, it is only parsed and validated once, which will often greatly improve performance. Prepared style sheets are thread-safe so they can be used to serve documents to several users concurrently.

The URLs for the source document and the stylesheet document are supplied in the URL, which will typically take the form:

http://server.com/servlets/SaxonServlet?source=doc.xml&style=sheet.xsl

Note: Internet Explorer assumes that if the URL ends with ".xml" or ".xsl", as in the above example, then the returned file will be XML - even if the media type in the HTTP header is set to "text/html". You can prevent this behaviour by adding an extra dummy parameter, for example "&x=y".

The source and style parameters identify the source document and stylesheet by URL. These are interpreted relative to the servlet context. This means that specifying say "style=/styles/styleone.xsl" in the URL will locate the stylesheet in this file relative to the root directory for the web server.

The stylesheet is prepared the first time it is used, and held in memory in a cache. The cache may be cleared (for example, if a stylesheet has been changed) using a URL such as:

http://server.com/servlets/SaxonServlet?clear-stylesheet-cache=yes

This code is provided purely as a sample, in the expectation that you will customise it to your particular site requirements.

Also see SaxonSAServlet.java for a schema-aware version of this servlet, and XPathExampleServlet.java which illustrates the use of the JAXP XPath API within a Java servlet.