You can modify the response object after a target resource is accessed by adding or updating response headers. You can also override request methods (such as the request.getWriter method) by wrapping the response in objects that extend either the ServletResponseWrapper or HttpServletResponseWrapper.
To customize responses, you can use the request wrapper class. For more information on wrapper classes, see "Using wrappers". This section shows an example of a filter using a wrapper class to customize the response object.
The following example reads in the response output and replaces certain Strings. It appends a stock ticker symbol and hyperlink to stock information to any occurrences of the words "Macromedia" or "Yahoo".
The following sample filter code shows these activities:
In the chain.doFilter call, the filter passes the new wrapper rather than the response object.
package jrunsamples.filters; import java.io.*;
import java.util.*; import javax.servlet.*; import javax.servlet.http.*;
public final class ParseFilter extends GenericFilter {
�public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
��ServletContext context = filterConfig.getServletContext();
��PrintWriter out = response.getWriter();
��HttpServletResponse resp = (HttpServletResponse)response;
��CharWrapper wrapper = new CharWrapper(resp);
��chain.doFilter(request, wrapper);
��String temp = wrapper.toString();
��StringTokenizer st = new StringTokenizer(temp," \t\n\r");
��while (st.hasMoreTokens()) {
���String word = (String) st.nextToken();
���if (word.equals("Macromedia")) {
����word = word + " (<A HREF=\"http://finance.yahoo.com/q?s=macr\">MACR</A>)";
���} else if (word.equals("Yahoo")) {
����word = word + " (<A HREF=\"http://finance.yahoo.com/q?s=yhoo\">YHOO</A>)";
���}
���out.write(" " + word + " ");
��}
��response.setContentLength(temp.length());
��out.close();
�}
}
The following class, CharWrapper, extends HttpServletResponseWrapper. It shows the following activities:
getWriter method with a constructor using the CharArrayWriter. Instead of overriding getWriter, you can override getOutputStream, but you cannot override both.
toString method to capture toString calls and output them using the CharArrayWriter.//based on Sun's CharResponseWrapper class in "The Essentials of Filters" package jrunsamples.filters; import java.io.*;
import javax.servlet.*; import javax.servlet.http.*;
public class CharWrapper extends HttpServletResponseWrapper {
�private CharArrayWriter cawout;
�public CharWrapper(HttpServletResponse response){
��super(response); ��cawout = new CharArrayWriter(); �}
�public String toString() {
��return cawout.toString(); �}
�public PrintWriter getWriter(){
��return new PrintWriter(cawout); �} }
In the web application's web.xml deployment descriptor, define the filter and add its mapping; for example:
<filter>
<filter-name>ParseFilter</filter-name> <filter-class>jrunsamples.filters.ParseFilter</filter-class> </filter>
<filter-mapping>
<filter-name>ParseFilter</filter-name> <url-pattern>/macromedia.html</url-pattern> </filter-mapping>
For related examples of filters that modify the output of servlets, see the links listed in "Resources".
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/filters6.htm