文档介绍:Advanced Servlets and Java Server
Pages[tm] Programming
Doris Chen .
Staff Engineer
Technology Evangelism
Sun Microsystems, Inc.
Chapter 3
Servlet Filters
Objectives
● Understand the fundamentals of filters
● Learn how to write filters
● Learn how to deploy filters
Java Servlet —Filters
● ponent framework
● Dynamically intercepting and modifying
requests and responses
● Apply filters to ponents or pages
● Allows range of activities:
– Marking access, blocking access
– Content transformations
● Works on and above
What is a Filter?
● A reusable piece of code
● Transform the content of HTTP requests,
response, and header information
● Do not generally create response or
responding to requests
● Attached to one or more servlets or JSP™
● Modify or adapt the request/response for a
resource
● Act on dynamic or static content – web
resources
What Can a Filter Do?
● Access a resource before a request is invoked
● Process a request before it is invoked
● Modify request headers and data
● Modify response headers and data before
sending them to client
● Intercept an invocation of a resource
● Act on a servlet, or servlets or static content
by one or more filters in a specific order
Importance and Benefits
● Provide the ability to encapsulate recurring
tasks in reusable units
– Developers have ways to modularize the code
– Code is more manageable, documentable, reusable
and easy to debug
● Separate high-level access decisions from
presentation code
– Especially valuable with JSP
– Separate business logic from presentation
● Apply wholesale changes to many different
resources
Examples of Filters
● Authentication
– Blocking requests based on user identity
● Logging and auditing
– Tracking user of a web application
● Image conversion
– Scaling maps, and so on
● Localization
– Targeting the request and response to a particular
locale
● XSLT transformations