| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Resin provides a set of convenient filters in the com.caucho.filters.* package.
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. GzipFilter is available in Resin-Professional.
The XsltFilter transforms the response using xslt. A Servlet or a JSP can produce XML results which are transformed using xslt.
See class com.caucho.servlets.XsltFilter . A request attribute or xml processing directive specifies the stylesheetResin's XsltFilter determines the stylesheet (*.xsl file) to apply from the first of:
The classpath is used to find the stylesheet. A stylesheet can be placed in WEB-INF/classes/, or a specific xsl directory can be indicated:
In a JSP the request attribute can be set with the following:
Specifying a processing instruction is another way to indicate the stylesheet (but a bit slower for performance considerations):
The stylesheet is applied only for certain content typesThe filter by default will only transform responses that have a content type of one of the following:
The filter can also be configured to unconditionally do a transformation regardless of the content type:
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.
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.
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 .
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.
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.
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 which matches against the URL and a which specifies the new URL to be forwarded to. Multiple <rewrite> tags are allowed.
The ThrottleFilter filter implemented with class com.caucho.filters.ThrottleFilter restricts the number of requests from the same IP, defaulting to 2 (the HTTP spec limit.) The ThrottleFilter is useful to limit some parallel download programs that can use more threads than they should.
|