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> */