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 274 | Next

Larry Brown, Marty Hall, and Yaakov Chaikin

"Core Servlets and JavaServer Pages, Volume 2"

Use of this class involves five steps:
1. Create a response wrapper. Extend javax.servlet.http.
HttpServletResponseWrapper.
2. Provide a PrintWriter and a ServletOutputStream that
buffer output. Override the getWriter and getOutputStream
methods to return a PrintWriter and a ServletOutputStream
that save everything sent to them and store that result in a field that
can be accessed later. The reason we need to override both get-
Writer and getOutputStream is because the servlet providing the
actual output is free to use either getWriter or getOutputStream
methods. However, remember that it is illegal to call both of those
methods on the same response object, so we are guaranteed that only
one of those methods will be called.
3. Pass that wrapper to doFilter. This call is legal because Http-
ServletResponseWrapper implements HttpServletResponse.
4. Extract and modify the output. After the call to the doFilter
method of the FilterChain, the output of the original resource is
available to you through whatever mechanism you provided in Step 2.
You can modify or replace it as appropriate for your application.
5. Send the modified output to the client. Because the original resource
no longer sends output to the client (the output is stored in your response
wrapper instead), you have to send the output. Your filter needs to obtain
the PrintWriter or ServletOutputStream from the original
response object and pass the modified output to that stream.


Pages:
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286