*/
public String toString() {
return(stringWriter.toString());
}
/** Get the underlying StringBuffer. */
public StringBuffer getBuffer() {
return(stringWriter.getBuffer());
}
}
Listing 5.15 StringWrapper.java (continued)
5.10 Example: A Replacement Filter 237
5.10 Example: A Replacement Filter
This section presents one common application of the StringWrapper shown in the
previous section: a filter that changes all occurrences of a target string to some
replacement string.
A Generic Modification Filter
Listing 5.17 presents a filter that wraps the response in a StringWrapper, passes
that wrapper to the doFilter method of the FilterChain object, extracts a
String that represents all of the resource??™s output, and calls the doModification
method, passing it the original output string. The doModification method makes
changes to the original output and returns the modified output string. It is the modified
output that gets actually sent to the client.
There is one thing to note about this filter??”it is an abstract class. To use it, you
must create a subclass that provides the implementation of the doModification
method. This setup allows us to adapt this generic modification filter to whatever our
Listing 5.16 StringOutputStream.java
package coreservlets.filters;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** StringOutputStream is a stub for ServletOutputStream which
* buffers up the output in a string buffer instead of sending it
* straight to the client.
Pages:
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289