10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Programming Customized Requests and Responses<br />

<strong>The</strong>re are many ways for a filter to modify a request or response. For example, a filter can add an<br />

attribute to the request or can insert data in the response. In the Duke’s Bookstore example,<br />

HitCounterFilter inserts the value of the counter into the response.<br />

A filter that modifies a response must usually capture the response before it is returned to the<br />

client. To do this, you pass a stand-in stream to the servlet that generates the response. <strong>The</strong><br />

stand-in stream prevents the servlet from closing the original response stream when it<br />

completes and allows the filter to modify the servlet’s response.<br />

To pass this stand-in stream to the servlet, the filter creates a response wrapper that overrides<br />

the getWriter or getOutputStream method to return this stand-in stream. <strong>The</strong> wrapper is<br />

passed to the doFilter method of the filter chain. Wrapper methods default to calling through<br />

to the wrapped request or response object. This approach follows the well-known Wrapper or<br />

Decorator pattern described in Design Patterns, Elements of Reusable Object-Oriented <strong>Software</strong>,<br />

by Erich Gamma et al. (Addison-Wesley, 1995). <strong>The</strong> following sections describe how the hit<br />

counter filter described earlier and other types of filters use wrappers.<br />

To override request methods, you wrap the request in an object that extends<br />

ServletRequestWrapper or HttpServletRequestWrapper. To override response methods, you<br />

wrap the response in an object that extends ServletResponseWrapper or<br />

HttpServletResponseWrapper.<br />

HitCounterFilter wraps the response in a<br />

tut-install/javaeetutorial5/examples/web/bookstore1/src/java/com/sun/bookstore1/filters/<br />

<strong>The</strong> wrapped response is passed to the next object in the filter chain, which is<br />

BookStoreServlet. <strong>The</strong>n BookStoreServlet writes its response into the stream created by<br />

CharResponseWrapper. When chain.doFilter returns, HitCounterFilter retrieves the<br />

servlet’s response from PrintWriter and writes it to a buffer. <strong>The</strong> filter inserts the value of the<br />

counter into the buffer, resets the content length header of the response, and then writes the<br />

contents of the buffer to the response stream.<br />

PrintWriter out = response.getWriter();<br />

CharResponseWrapper wrapper = new CharResponseWrapper(<br />

(HttpServletResponse)response);<br />

chain.doFilter(request, wrapper);<br />

CharArrayWriter caw = new CharArrayWriter();<br />

caw.write(wrapper.toString().substring(0,<br />

wrapper.toString().indexOf("")-1));<br />

caw.write("\n" +<br />

messages.getString("Visitor") +"" +<br />

counter.getCounter() + "");<br />

caw.write("\n");<br />

response.setContentLength(caw.toString().getBytes().length);<br />

out.write(caw.toString());<br />

Filtering Requests and Responses<br />

Chapter 4 • <strong>Java</strong> ServletTechnology 117

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!