* First, this constructor calls the parent
* constructor. That call is crucial so that the response
* is stored and thus setHeader, setStatus, addCookie,
* and so forth work normally.
*
* Second, this constructor creates a StringWriter
* that will be used to accumulate the response.
*/
Chapter 5 ?– Servlet and JSP Filters 236
public StringWrapper(HttpServletResponse response) {
super(response);
stringWriter = new StringWriter();
}
/** When servlets or JSP pages ask for the Writer,
* don't give them the real one. Instead, give them
* a version that writes into the StringBuffer.
* The filter needs to send the contents of the
* buffer to the client (usually after modifying it).
*/
public PrintWriter getWriter() {
return(new PrintWriter(stringWriter));
}
/** Similarly, when resources call getOutputStream,
* give them a phony output stream that just
* buffers up the output.
*/
public ServletOutputStream getOutputStream() {
return(new StringOutputStream(stringWriter));
}
/** Get a String representation of the entire buffer.
*
* Be sure not to call this method multiple times
* on the same wrapper. The API for StringWriter
* does not guarantee that it "remembers" the previous
* value, so the call is likely to make a new String
* every time.
Pages:
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288