Should filters write to the response during or after filtering?
Posted
by Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2010-04-21T15:22:30Z
Indexed on
2010/04/21
15:23 UTC
Read the original article
Hit count: 183
I have a filter which processes generated HTML and rewrites certain elements. For example, it adds class attributes to some anchors. Finally, it writes the processed HTML to the response (a subclass of HttpServletResponseWrapper). Naturally, this means that the processed HTML is a different length after it has passed through the filter.
I can see two ways of approaching this.
One is to iterate over the HTML, using a StringBuilder to build up the processed HTML, and write the processed HTML to the response once all filtering is complete. The other is to iterate over the HTML but to write it to the response as soon as each element has been processed.
Which is the better way for this operation, or is there another option which would be preferable? I am looking to minimise temporary memory usage primarily.
© Stack Overflow or respective owner