Stylesheet Caching Revisited

We have seen two approaches that eliminate the need to hardcode the absolute pathname of XSLT stylesheets in your servlet code. In the first approach, the ServletContext was used to load resources from the web application using a relative pathname. In the second approach, the location was specified as a context initialization parameter.

This takes care of compilation changes, but now we have the issue of dynamic loading. In the PersonalDataServlet class, the two XSLT stylesheets are located and “compiled” into instances of the javax.xml.transform.Templates interface. Although this offers high performance for transformations, the two stylesheets are never flushed from memory. If changes are made to the XSLT stylesheets on disk, the servlet must be stopped and started again.

Integration with the Stylesheet Cache

In Chapter 5, a stylesheet cache was implemented. In this next example, PersonalDataServlet is modified to use the cache instead of Templates directly. This will offer virtually the same runtime performance. However, you will be able to modify the stylesheets and immediately see those changes in your web browser. Each time a stylesheet is requested, the cache will check its timestamp on the file system. If the file has been modified, a new Templates instance is instantiated without bringing down the servlet.

Fortunately, integration with the cache actually makes the PersonalDataServlet simpler to implement. Example 6-10 contains the modified ...

Get Java and XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.