Datasheet
Patrick c01.tex V3 - 09/18/2009 12:15pm Page 23
Chapter 1: Building Web Applications in WebLogic
pages and servlets using either entries in the
web.xml
descriptor file or annotations in the filter class. To
illustrate this process, we will build and deploy a simple but useful filter that intercepts servlet and JSP
requests and logs
HttpServletRequest
information before passing the request on to the intended JSP
page or servlet.
Building a Simple SnoopFilter Filter Class
The first step is the construction of a filter class called
SnoopFilter
that implements the
javax.servlet.Filter
interface and performs the desired logging of request information. Sim-
ply put, the
doFilter()
method writes information from the
HttpServletRequest
object to
System.out
before forwarding to any additional filters in the filter chain or to the final destination page itself. The
source for
SnoopFilter
is available from the companion web site (
http://www.wrox.com/
).
Registering SnoopFilter in the Application
Registering a filter normally requires a set of elements in the web application descriptor file,
web.xml
.
These elements declare the filter class and define the pages or servlets to which the filter should be
applied. In this simple example, you want all pages and servlets in the application to be filtered through
SnoopFilter
,andthe
web.xml
file includes the following elements:
<filter>
<filter-name>SnoopFilter</filter-name>
<display-name>SnoopFilter</display-name>
<description></description>
<filter-class>
professional.weblogic.ch01.example1.SnoopFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>SnoopFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The
<url-pattern>/*</url-pattern>
element declares that all pages and servlets in the applica-
tion should be filtered using
SnoopFilter
, so every page request will go through the filter before
normal processing begins. The server’s
stdout
stream will therefore contain detailed request
information for every page request, which is potentially very useful during development and
debugging.
Clearly the same general logging capability could have been placed in a helper class, custom tag, or
simple scriptlet included in each JSP page or servlet, but the ability to control the specific pages or groups
of pages using the
SnoopFilter
in a declarative manner (via
<url-pattern>
elements) has significant
advantages.
WebLogic Server also supports an annotation-based approach for registering a filter and specifying the
URL pattern. The following
@WLFilter
syntax is equivalent to the
web.xml
entries shown above:
@WLFilter (
name = "SnoopFilter",
mapping = {"/*"}
23