Getting HTML content from a ServletFilter

Does anyone have experience viewing the HTML content from a ServletFilter after-dispatch? The response seems to be a SelectingResponseWrapper, but I can’t seem to locate where the output HTML is actually located?

Hi Daniel!

What is it exactly that you’re trying to do? Also, did you mean before-dispatch? (I don’t see any after-dispatch in the docs.)


You’re right, I should have written that I’m looking at either the “before-decoration” which gives me a com.opensymphony.sitemesh.webapp.ContentBufferingResponse for the response object or “after-encoding” which seems to give me a com.atlassian.gzipfilter.GzipResponseWrapper.

Either way, I can’t find where I might check the content of the page/servlet which is about to be sent to the client. I’m trying to create a filter which blocks/redirects the client if the server is about to transmit problematic content (i.e. PII/security information).

Based on the sheer number of macros and other “modifiers” that might be part of a page, I’m trying to think of the fastest way to intercept a response without slowing down the whole application.


The HttpServlet interface is designed to be streamy, so I speculate that there’s no place designed to be able to extract the sent content (or at least not more than once). And the actual object returned by Confluence is obviously an implementation detail and subject to change anyway.

What you can do is create a <servlet-filter> that runs before-dispatch. Before you call chain.doFilter(), you can replace the response object with your own proxy that returns a custom OutputStream.

From that point, you can store the content as a string for examination once you return from the filter chain. You could also possibly try to filter the information in-place (saving you from having to store the entire page in memory and accelerating time-to-first-byte), but remember that once you’ve passed the content to the output stream of the parent response object, you are committed to sending it.


Thanks for pointing me in the right direction, eventually I realized I could intercept the PrintWriter as part of the chain.doFilter()