Hi have you seen anything really useful extending HttpServletResponseWrapper
/HttpServletRequestWrapper
or ServletRequestWrapper
/ ServletResponseWrapper
in production environment?
-
2This should probably be community wiki – skaffman Feb 17 '10 at 08:25
5 Answers
I've used it to "capture" the output of a JSP, so that it can be transformed by the forwarding controller. This was done by overriding the getWriter()
/ getOutputStream()
methods.
SiteMesh does a similar thing, capturing the "target" in order to decorate the response.

- 398,947
- 96
- 818
- 769
- Trim whitespace from generated HTML, can save over 50% of bandwidth. I've had a project which displays large tabular data (with nicely indented HTML source), response size went from ~200KB to ~70KB by just trimming all whitespace away.
- Process multipart/form-data requests transparently by putting multipart form data back in request parameter map so that you can use
request.getParameter()
and consorts the usual way again (Servlet <=2.5 example, Servlet >=3.0 example). - Capture the outputstream, either to have a copy or to MD5-hash it.
- Disabling URL rewriting.
Etcetera.. Etcetera.. All just to override and change the default behaviour of certain HttpServletRequest
/HttpServletResponse
methods.
We use it to
overwrite
getRemoteAddr()
to return values fromX-Forwarded-For
orX-Real-IP
(set by our nginx proxy)filter certain headers, e.g. to avoid content negotiation in 3rd party servlets
gzip response

- 35,575
- 15
- 95
- 119
-
GZIP is just configreable in any decent servlet container. In Tomcat for example, just set `
`: http://tomcat.apache.org/tomcat-6.0-doc/config/http.html – BalusC Feb 17 '10 at 12:16 -
@balusc we've been using embedded Jetty. see http://docs.codehaus.org/display/JETTY/GZIP+Compression and http://jetty.codehaus.org/jetty/jetty-6/xref/org/mortbay/servlet/GzipFilter.html – sfussenegger Feb 17 '10 at 13:24
Many frameworks (richfaces, myfaces on my current classpath at least) use a request wrapper to handle multipart requests
Spring has
ContextExposingHttpServletRequest
so that spring beans are accessible as request attributes.myfaces orchestra uses the response wrapper to rewrite the URL in order to ensure multiple-window conversation context

- 588,226
- 146
- 1,060
- 1,140
I've used it just recently to capture outgoing response headers for debugging purposes (getHeaderNames()
and getHeader()
were only added on the response side in the Servlet 3.0 spec.
We've also used it for gathering metrics on our servlets, by overriding getOutputStream()
to return an OutputStream
implementation which increases a counter on every byte that passes through it so we can tell what our high-network-traffic servlets are.

- 37,227
- 11
- 66
- 65