caucho
Resin
FAQ
Reference Guide
Demo
Tutorial

JSP page
Config
URLs
Database Forms
XTP Copy
Hello Tag
Vary Filter
HardCore
Mailing Forms
Beans
Cache
XSL Filter
run-at
 Servlet Filter with XTP -- Vary

Connection Tag
Tutorial
HardCore

The Servlet 2.3 specification creates a new way to control servlets: filters. Filters can inspect and modify the request or response before and after passing the request to the servlet.

This example uses a servlet filter to select a stylesheet Serif (XTP) pages. If the user adds the style=plain query, the page will use a plain stylesheet. Otherwise the page will use a fancy stylesheet.

The filter itself just looks at the "style" parameter. If it's "plain", then the filter will set the caucho.xsl.stylesheet parameter to plain.xsl. Serif uses caucho.xsl.stylesheet to transform the page to HTML.

test.VaryFilter.java
package test;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class VaryFilter implements Filter {
  private FilterConfig config;

  public void init(FilterConfig config)
  {
    this.config = config;
  }

  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain next)
    throws IOException, ServletException
  {
    HttpServletRequest req = (HttpServletRequest) request;

    String style = req.getParameter("style");

    if ("plain".equals(style))
      req.setAttribute("caucho.xsl.stylesheet", "plain.xsl");
    else
      req.setAttribute("caucho.xsl.stylesheet", "default.xsl");

    next.doFilter(request, response);
  }

  public void destroy()
  {
  }
}
web.xml
<web-app>
  <filter-mapping url-pattern='*.xtp'
                  filter-name='test.VaryFilter'/>
</web-app>

The sample Serif page has a small section and some content. It's parsed as HTML before using the stylesheets. The default stylesheet will color the section header red. The plain stylesheet leaves it as black.

test.xtp
<title>A Sample Title</title>

<s1 title="A Sample Section">

Some content in the sample section.

</s1>

Unknown tags are copied from the Serif page to the generated HTML page unchanged. So you can just add rules for tags you want to change. The example forces the background of the body to white and formats a section with an H3 header colored red.

default.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <!-- make sure '<' is not printed as '&lt;' -->
  <xsl:output disable-output-escaping='true'/>

  <!-- copy input to output -->
  <xsl:template match='*|@*'>
    <xsl:copy>
      <xsl:apply-templates select='node()|@*'/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="body">
    <body bgcolor=white>
      <xsl:apply-templates/>
    </body>
  </xsl:template>

  <xsl:template match="s1">
    <h3><font color=red><xsl:value-of select="@title"/></font></h3>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

The plain stylesheet leaves the Serif page untouched except for converting the section title to use plain H3.

plain.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <!-- make sure '<' is not printed as '&lt;' -->
  <xsl:output disable-output-escaping='true'/>

  <!-- copy input to output -->
  <xsl:template match='*|@*'>
    <xsl:copy>
      <xsl:apply-templates select='node()|@*'/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="s1">
    <h3><xsl:value-of select="@title"/></h3>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

Connection Tag
Tutorial
HardCore
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.