开发者

ServletRequestWrapper or ServletResponseWrapper in production?

Hi have you seen anything really use开发者_运维知识库ful extending HttpServletResponseWrapper/HttpServletRequestWrapper or ServletRequestWrapper/ ServletResponseWrapper in production environment?


  • 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.


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.


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


  • 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜