Datasheet

Patrick c01.tex V3 - 09/18/2009 12:15pm Page 24
Chapter 1: Building Web Applications in WebLogic
)
public class SnoopFilter implements Filter
{
...
}
Although this is obviously a simple example,
SnoopFilter
illustrates the value of filters for prepro-
cessing activities such as logging, auditing, or debugging in Java EE web applications. Filters are not
limited to writing output to
stdout
; they can easily write information to separate log files, insert rows
in database tables, call EJB components, add or modify request attributes, forward the page request
to a different web application component, or perform any other desired behavior unconditionally
or based on specific request information. They are a very powerful tool in the Java EE servlet
specification.
Best Practice
Use filters to implement common behaviors such as logging, auditing, and security
verification for servlets and JSP pages in your web applications.
Response Caching Using the CacheFilter
WebLogic Server includes a filter called
CacheFilter
that provides page-level response caching for web
applications. This filter operates at the complete page level rather than surrounding and caching only
a section of JSP content in a page. The
CacheFilter
may also be used with servlets and static content,
unlike the related
wl:cache
custom tag, which works only in JSP pages.
The
CacheFilter
is registered like any other servlet filter. Define the filter in the
web.xml
file, and
specify the
<url-pattern>
of the page or pages to cache. Use initialization parameters in the filter
registration to define timeout criteria and other cache control values. For example, to cache the
response from a specific JSP page for 60 seconds, register the
CacheFilter
using elements similar to
the following:
<filter>
<filter-name>CacheFilter1</filter-name>
<filter-class>weblogic.cache.filter.CacheFilter</filter-class>
<init-param>
<param-name>timeout</param-name>
<param-value>60</param-value>
</init-param>
</filter>
...
<filter-mapping>
<filter-name>CacheFilter1</filter-name>
<url-pattern>CacheFilterTest1.jsp</url-pattern>
</filter-mapping>
The
CacheFilterTest1.jsp
page will execute the first time the URL is accessed by any client, and the
content of the HTTP response will be cached by the filter and used for all subsequent access requests for
60 seconds.
24