tags:

views:

209

answers:

5

Hi have you seen anything really useful extending HttpServletResponseWrapper/HttpServletRequestWrapper or ServletRequestWrapper/ ServletResponseWrapper in production environment?

+1  A: 

We use it to

  • overwrite getRemoteAddr() to return values from X-Forwarded-For or X-Real-IP (set by our nginx proxy)

  • filter certain headers, e.g. to avoid content negotiation in 3rd party servlets

  • gzip response

sfussenegger
GZIP is just configreable in any decent servlet container. In Tomcat for example, just set `<Connector compression="on">`: http://tomcat.apache.org/tomcat-6.0-doc/config/http.html
BalusC
@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
+1  A: 
  • 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

Bozho
+2  A: 

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.

skaffman
+1  A: 

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.

Cowan
A: 
  • 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.

BalusC