Filter that uses a response wrapper to convert all output to uppercase : Filter « Servlet « Java Tutorial






import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class UpperCaseFilter implements Filter {
  protected FilterConfig config;

  public void init(FilterConfig config) throws ServletException {
    this.config = config;
  }

  public void destroy() {
  }

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws ServletException, IOException {
    ServletResponse newResponse = response;

    if (request instanceof HttpServletRequest) {
      newResponse = new CharResponseWrapper((HttpServletResponse) response);
    }

    chain.doFilter(request, newResponse);

    if (newResponse instanceof CharResponseWrapper) {
      String text = newResponse.toString();
      if (text != null) {
        text = text.toUpperCase();
        response.getWriter().write(text);
      }
    }
  }
}

class CharResponseWrapper extends HttpServletResponseWrapper {
  protected CharArrayWriter charWriter;

  protected PrintWriter writer;

  protected boolean getOutputStreamCalled;

  protected boolean getWriterCalled;

  public CharResponseWrapper(HttpServletResponse response) {
    super(response);

    charWriter = new CharArrayWriter();
  }

  public ServletOutputStream getOutputStream() throws IOException {
    if (getWriterCalled) {
      throw new IllegalStateException("getWriter already called");
    }

    getOutputStreamCalled = true;
    return super.getOutputStream();
  }

  public PrintWriter getWriter() throws IOException {
    if (writer != null) {
      return writer;
    }
    if (getOutputStreamCalled) {
      throw new IllegalStateException("getOutputStream already called");
    }
    getWriterCalled = true;
    writer = new PrintWriter(charWriter);
    return writer;
  }

  public String toString() {
    String s = null;

    if (writer != null) {
      s = charWriter.toString();
    }
    return s;
  }
}

Simple response wrapper that utilizes a local CharArrayWriter for output


<filter>
  <filter-name>Upper Case Filter</filter-name>
  <filter-class>com.omh.filters.UpperCaseFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>Upper Case Filter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

*/








25.17.Filter
25.17.1.Your First Filter
25.17.2.Filter Response
25.17.3.Servlet Filter Init Parameter
25.17.4.Use Filter to log Configuration
25.17.5.Use Filter to check Request Parameter
25.17.6.Use Filter to Check Date
25.17.7.Simple filter for measuring servlet response times
25.17.8.Filter that uses a response wrapper to convert all output to uppercase