|
|
 |
|
Servlet Filter with XTP -- Vary
|

The draft 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>
<section title="A Sample Section">
Some content in the sample section.
</section>
|
The example uses StyleScript as the style language. You can
easily translate the example to use string XSL syntax.
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
$output(disable-output-escaping=>true);
*|@* << $copy() << $apply-templates(node()|@*); >> >>
body << <body bgcolor=white> $apply-templates(); </body> >>
section << <h3><font color=red>$(@title)</font></h3> $apply-templates(); >>
|
The plain stylesheet leaves the Serif page untouched except for
converting the section title to use plain H3.
plain.xsl
$output(disable-output-escaping=>true);
*|@* << $copy() << $apply-templates(node()|@*); >> >>
section << <h3>$(@title)</h3> $apply-templates(); >>
|
Copyright © 1998-2001 Caucho Technology. All rights reserved.
Copyright © 1998-2001 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark,
and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.
|
 |
|