11) and apply it to all 30 resources.
Second, it lets you separate high-level access decisions from presentation code.
This is particularly valuable with JSP, where you usually want to keep the page almost
entirely focused on presentation, not processing business logic. For example, do you
want to block access from certain sites without modifying the individual pages to
Chapter 5 ?– Servlet and JSP Filters 204
which these access restrictions apply? No problem: Create an access restriction filter
(Section 5.8) and apply it to as many or few pages as you like.
Finally, filters let you apply wholesale changes to many different resources. Do
you have a bunch of existing resources that should remain unchanged except that the
company name should be changed? No problem: Make a string replacement filter
(Section 5.10) and apply it wherever appropriate.
5.1 Creating Basic Filters
Creating a filter involves five basic steps:
1. Create a class that implements the Filter interface. Your class
will need three methods: doFilter, init, and destroy. The
doFilter method contains the main filtering code (see Step 2), the
init method performs setup operations, and the destroy method
does cleanup.
2. Put the filtering behavior in the doFilter method. The first
argument to the doFilter method is a ServletRequest object.
This object gives your filter full access to the incoming information,
including form data, cookies, and HTTP request headers.
Pages:
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252