Graphic Java
Servlet filters

David Geary
Listing 1. /WEB-INF/classes/XslFilter.java.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import beans.xsl.XslProcessorBean;

public class XslFilter implements Filter {
   private FilterConfig config;

   public void doFilter(ServletRequest request,
                        ServletResponse response,
                        FilterChain chain)
                  throws ServletException, java.io.IOException {
      XslProcessorBean    xslBean = new XslProcessorBean();
      ServletContext      context = config.getServletContext();
      HttpServletRequest  hrequest= (HttpServletRequest)request;
      String                 path = hrequest.getPathTranslated();
      String           stylesheet = getStylesheet(request, path);

      xslBean.process(new FileReader(new File(path)),
                      request, response);
   public void setFilterConfig(FilterConfig config) {
      this.config = config;
   public FilterConfig getFilterConfig() {
      return config;
   private String getStylesheet(ServletRequest request,
                           String path) throws ServletException {
      String stylesheet = request.getParameter(‘stylesheet’);

      if(stylesheet == null) {
         String msg = ‘Please specify a stylesheet, like ‘ +
                      ‘this: ‘ + path + ‘?stylesheet.xsl’;

         throw new ServletException(msg);
      return stylesheet;

About the Author

David Geary was the lead engineer for the Java Management API GUI toolkit, and is now an independent Java consultant. David is the author of Graphic Java—Mastering the JFC. He can be contacted at [email protected].