Filter Library
Resin

Change Log
Documentation

Orientation
Features
Installation
Configuration
Web Applications
JSP
Servlets and Filters
Databases
Admin (JMX)
Security
XML and XSLT
XTP
Resources (JNDI)
Performance
Protocols
Third-party
Troubleshooting

Servlets
Servlet Lib
run-at
Filters
Filter Lib
Tutorials
Filters
Servlets and Filters
Tutorials

Resin provides a set of convenient filters in the com.caucho.filters.* package.

  1. GzipFilter
  2. XsltFilter
  3. TransactionFilter
  4. ExpiresFilter
  5. AnonymousExpiresFilter
  6. RewriteFilter
  7. PasswordFilter

GzipFilter

The GzipFilter compresses the output of pages for browsers which understand compression, and leaves the output unchanged if the browser does not support compression. A browser indicates to the server that it supports gzip compression by including "gzip" in the "Accept-Encoding" request header.

ParameterMeaningdefault
use-varySet the standard HTTP "Vary" response header to "Accept-Encoding". This indicates to the browser and any intervening cache that the response from this url might be different depending on the Accept-Encoding provided by the browser.true
no-cacheForbid the browser and any intervening cache from caching the results of this request. Sets the "Cache-Control" response header to "no-cache". If use-vary is false then this is always true.false
embed-error-in-outputEmbed the stack trace of any exception into the output stream that is being sent to the browser.false

<web-app xmlns="http://caucho.com/ns/resin">
  <filter filter-name="gzip"
          filter-class="com.caucho.filters.GzipFilter"/>

  <filter-mapping url-pattern="/*" filter-name="gzip"/>
</web-app>
See class com.caucho.servlets.GzipFilter .

XsltFilter

The XsltFilter transforms the response using xslt. A Servlet or a JSP can produce XML results which are transformed using xslt.

The stylesheet that is applied is determined in the following way (first one found is used):

  1. The value of request.getAttribute("caucho.xsl.stylesheet")
  2. A stylesheet specified in the source document with <?xml-stylesheet href='...'?>
  3. default.xsl

<web-app xmlns="http://caucho.com/ns/resin">
  <filter filter-name="xslt"
          filter-class="com.caucho.filters.XsltFilter"/>

  <filter-mapping url-pattern="/xslt/*" filter-name="xslt"/>
</web-app>
See class com.caucho.servlets.XsltFilter .

TransactionFilter

The TransactionFilter wraps the request in a UserTransaction and commits the transaction when the servlet completes. All database calls for the request will either succeed together or fail. The UserTransaction is obtained by doing a jndi lookup with the name "java:comp/UserTransaction".

This filter will gracefully handle any exceptions that occur during the request by doing a rollback on the transaction.

<web-app xmlns="http://caucho.com/ns/resin">
  <filter filter-name='transaction-filter'
          filter-class='com.caucho.filters.TransactionFilter'/>

  <filter-mapping url-pattern='/DatabaseServlet/*'
                  filter-name='transaction-filter'/>
</web-app>
See class com.caucho.servlets.TransactionFilter .

ExpiresFilter

The ExpiresFilter sets the Expires cache control header, allowing servlet output and jsp results to be cached for a short time.

This is useful for indicating an Expires time to the browser, and it is even more useful when used in conjunction with Resin's HTTP proxy cache . If Resin's HTTP proxy cache is in use, even a short Expires time (like 2s) can result in performance gains.

ParameterMeaningdefault
cache-timeThe amount of time before the servlet output will be requested again. In seconds (2s), minutes (2m), hours (2h), or days (2D).2s

Caching stock quotes for 60 seconds
<web-app xmlns="http://caucho.com/ns/resin">
  <filter filter-name='expires-60s'
          filter-class='com.caucho.filters.ExpiresFilter'>
    <init>
      <cache-time>60s</cache-time>
    </init>
  </filter>

  <filter-mapping servlet-name='StockQuoteServlet'
                  filter-name='expires-60s'/>
</web-app>

In this example, the StockQuoteServlet will be only called once every 60 seconds. This indicates to a browser that it should refresh it's local cache of the url after 60 seconds have passed. If Resin's HTTP proxy cache is being used, the first request from any browser will cause execution of the StockQuoteServlet, any requests from any browser for the next 60 seconds will be served from the proxy cache (the servlet will not be called).

See class com.caucho.servlets.ExpiresFilter .

AnonymousExpiresFilter

The AnonymousExpiresFilter caches the response for anonymous users. A user is anonymous if the do not have a session. When a page has custom formatting for logged in users, it may still want to cache the results for non-logged in users saving time and database access.

The benefits of using this filter are similar to those described in ExpiresFilter.

ParameterMeaningdefault
cache-timeThe amount of time before the servlet output will be requested again. In seconds (2s), minutes (2m), hours (2h), or days (2D).2s

Servlets should call request.getSession(false) to get their sessions, because once a session is created the response will no longer be cached. For the same reason, JSP pages should set <jsp:directive.page session='false'/> for all pages that do not need a session.

Caching all anonymouse users *.jsp pages for 15 minutes
<web-app xmlns="http://caucho.com/ns/resin">
  <filter filter-name='anonymous-expires'
          filter-class='com.caucho.filters.AnonymousExpiresFilter'>
    <init>
      <cache-time>15m</cache-time>
    </init>
  </filter>

  <filter-mapping url-pattern='*.jsp'
                  filter-name='anonymous-expires'/>
</web-app>
See class com.caucho.servlets.AnonymousExpiresFilter .

RewriteFilter

The RewriteFilter rewrites and forwards URLs matching a regular expression. It is useful either when URLs change during a site redesign or when a site might want to hide its JSP structure. The functionality is similar to mod_rewrite in the Apache web server.

The RewriteFilter is configured with a list of <rewrite> tags. Each tag has a pattern which matches against the URL and a target which specifies the new URL to be forwarded to. Multiple <rewrite> tags are allowed.

RewriteFilter example
<filter filter-name='rewrite'
        filter-class='com.caucho.filters.RewriteFilter'>
  <init>
    <rewrite pattern="/a/([^/]*)/([^?]*)" target="/$2/$1.jsp"/>
    <rewrite pattern="/b/([^/]*)/([^?]*)" target="/$2/$1.html"/>
  </init>
</filter>

<filter-mapping url-pattern='/*' filter-name='rewrite'/>
See class com.caucho.servlets.RewriteFilter .

PasswordFilter

Store a password that the user submits as the session attribute java.naming.security.credentials. This is useful in situations where the web application needs the user's password to authenticate with some backend server, for example a mail server.

PasswordFilter configuration
<filter  filter-name='password' 
         filter-class='com.caucho.filters.PasswordFilter'/>

<filter-mapping url-pattern='j_security_check'
                filter-name='password'/>

Using the password stored by PasswordFilter
String password = null;
if (request.getUserPrincipal() != null)
  password = request.getSession().getAttribute("java.naming.security.credentials");
See class com.caucho.servlets.PasswordFilter .


Filters
Servlets and Filters
Tutorials
Copyright © 1998-2003 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.