Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Unheralded Java Filters Simplify Web-app Testing : Page 2

Java filters, the powerful—yet under-appreciated—components of the Java Servlet specification, can perform a range of useful tasks. Make them a part of your Java Web application toolkit.


advertisement
Getting Started with the Filter API
One of the nicest things about Java filters is that the API itself is short and simple. All told, it adds only three new interfaces and eight methods to the javax.servlet package. The three interfaces are Filter, FilterConfig, and FilterChain. FilterConfig's only job is to hold configuration information for the filter, which accesses this information by calling the getInitParameter() method.

FilterChain isn't complex either; it represents the stack—or "chain"—of filters that this request must pass through (a request may have to pass through many filters). It contains a single method, doFilter(), which you call to allow the request to pass down the stack to the next filter. When the last filter in the chain calls doFilter(), the servlet container invokes the requested JSP/servlet, or otherwise handles the request. As a filter developer, you must call doFilter() or the request will not continue its journey.

The Filter interface is where most of the action occurs. It consists of three methods: init(), doFilter(), and destroy(). Each method's logic is fairly transparent:

  • When the servlet container calls init(), it notifies the filter that it is about to be used.
  • The doFilter() method is called once for each request/response pair passed along the FilterChain.
  • The destroy() method notifies the filter that it is about to be taken out of service.



Go ahead and start your class, HTMLValidatorFilterLite, by supplying empty init() and destroy() methods after your initial imports and class definition:

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.xml.parsers.*; import org.w3c.dom.*; public final class HTMLValidatorFilterLite implements Filter { public void init(FilterConfig filterConfig) throws ServletException {} public void destroy() {}

All the work for your Filter takes place in the doFilter() method, which starts off with a few housekeeping tasks:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; PrintWriter out = res.getWriter();

For simplicity, cast the ServletRequest and ServletResponse objects to their HTTP cousins to avoid repeatedly having to cast in the code that follows. You then get a reference to the PrintWriter, which is the component that sends character text back to the client. You will use this object later to write data to the client.

Next, set up buffering for the HTTP response:

ResponseBuffer buffer = new ResponseBuffer(res);

ResponseBuffer is a wrapper around the ServletResponse object. The servlet API class HttpServletResponseWrapper, which ResponseBuffer extends, enables you to wrap the response. ResponseBuffer's only job is to buffer the output of this request. So as your application components write to the ServletResponse object, ResponseBuffer holds that data and keeps it from being sent to the client until you're ready.

Confused? Don't worry: this is a standard Java filters technique that you will see frequently. Just copy it when you need to buffer a response. You'll find the code for ResponseBuffer.java in the downloadable source code for this article.

Once you have started the buffering, send the request on to any other filters in the FilterChain, and then grab the results:

chain.doFilter(request, buffer); // lots of stuff can have happened! String contents = wrapper.toString();

With the call to doFilter(), the request is dispatched to the next filter. If no other filters are present, the request is dispatched to the requested servlet. That is, code in the filter before this call to doFilter() executes before the requested servlet executes, while code after this point executes after the requested servlet executes. It's as if your code steps through a rift in time—whole worlds of activity could take place after this method call. In this example, once you make the call to doFilter(), your ResponseBuffer fills and you can access the HTML content of the response in the very next line.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap