SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 285 | Next

Larry Brown, Marty Hall, and Yaakov Chaikin

"Core Servlets and JavaServer Pages, Volume 2"

However,
because we would like to be able to apply this behavior to multiple resources, a filter
is a much more appropriate place for it. The compression filter can use the String-
Wrapper of Section 5.9 (Modifying the Response) to compress content when the
browser supports such a capability. Accomplishing this task requires the following:
1. A class that implements the Filter interface. This class is called
CompressionFilter and is shown in Listing 5.21. The init
method stores the FilterConfig object in a field in case subclasses
need access to the servlet context or filter name. The body of the
destroy method is left empty.
2. A wrapped response object. The doFilter method checks if the
client indicates that it supports compression (i.e., has gzip as one of
the values of its Accept-Encoding header). If it doesn??™t, we invoke
the doFilter method of the FilterChain object with the original
response and request objects. If the client supports gzip compression,
the doFilter method wraps the ServletResponse object in
a StringWrapper and passes that wrapper to the doFilter
method of the FilterChain object. After this call completes, all
other filters and the final resource have executed and the output is
inside the wrapper. So the original doFilter extracts a String that
represents all of the resource??™s output. We then wrap a ByteArray-
Chapter 5 ?–  Servlet and JSP Filters 246
OutputStream in a GZIPOutputStream.


Pages:
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297