[ Team LiB ] Previous Section Next Section

Recipe 19.7 Filtering the HTTP Response


You want to change the response with a filter while the client request is en route to the servlet.


Change the javax.servlet.ServletResponse inside the filter's doFilter( ) method by wrapping the response with your own object. Then pass the wrapped response as a parameter into the FilterChain.doFilter( ) method.


Here are the steps for changing a response with a filter and a wrapper class:

  1. Create a Java class that extends javax.servlet.http. HttpServletResponseWrapper.

  2. Place this class, including its package-related directories, in WEB-INF/classes.

  3. Use the wrapper class in the filter to wrap the response object, which is a parameter to the filter's doFilter( ) method.

  4. Call the chain.doFilter( ) method with the wrapped response as a parameter.

Example 19-9 shows the Java class that we will use to wrap the response object.

If you are just making a simple response change, you do not have to go to the trouble of using an HttpServletResponseWrapper class. This code inside of a filter's method adds a header to the response, then calls the chain.doFilter( ) method with the altered response:

if(response instanceof HttpServletResponse){
    //cast to HttpServletResponse  to call 
    myHttpResponse =

         "BASIC realm=\"Admin\"");

   chain.doFilter(request,response); }

The ResponseWrapper class contains the skeleton of a new method named getWebResource. I want to show the mechanics of wrapping the response in a filter, so have kept this wrapper class very simple.

All the other HttpServletResponse-derived method calls are delegated to the wrapped response object, which is the convenience of extending HttpServletResponseWrapper.

Example 19-9. An HttpServletResponseWrapper class for use in a filter
package com.jspservletcookbook;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpServletResponse;

public class ResponseWrapper extends HttpServletResponseWrapper{
 public ResponseWrapper(HttpServletResponse response){
 public String getWebResource(String resourceName){
     //Implement a method to return a String representing 
     //the output of  a web resource
         //See Recipe 13.5
         return "resource"; //for the compiler...
 }// getWebResource

Example 19-10 shows the doFilter( ) method inside the filter that uses this ResponseWrapper class.

The class extending HttpServletResponseWrapper must be placed beneath WEB-INF/classes, with a directory structure that matches its package name.

Example 19-10. The doFilter( ) method of a filter that uses a HttpServletResponseWrapper class
public void  doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws java.io.IOException, ServletException {
    if(response instanceof HttpServletResponse){

            new ResponseWrapper((HttpServletResponse)response));

    } else {



The code calls the chain.doFilter( ) method and passes in the wrapped response as a parameter. The web resource at the end of the chain has access to the customized response object and can call the additional method the response wrapper class has defined. All the other method calls on the HttpServletResponse object, such as getWriter( ) or getOutputStream( ), are passed through to the wrapped response object.

See Also

Recipe 7.9 on using a filter to read request parameter values; Recipe 11.11 on using a filter to monitor session attributes; Recipe 18.3 on using a filter to alter the request; Recipe 19.1-Recipe 19.4 on mapping filters to web components; Recipe 19.5 on configuring init parameters for a filter; Recipe 19.6 on blocking a request; Recipe 19.8 on using filters with RequestDispatchers; Recipe 19.9 on using filters to check request parameters; Recipe 19.10 on using filters to disallow requests from certain IP addresses.

    [ Team LiB ] Previous Section Next Section