1

I am programming a Servlet-Filter, that wraps the response and analyzes calls to addHeader() of the wrapped response. Before the response is committed to the client, it decorates it with some usefull headers (caching,compression etc), if appropriate.

This decoration is done, when ServletResponse.flushBuffer() is called on the wrapped response, or when the buffer-size is reached. But if the wrapped Servlet (or JSP-Page) is processing an include at that moment, no headers can be added to the response, because an include is forbidden to do that.

Therefore, I need to detect includes, so that I am able, to decorate the repsonse before the ressource is included.

The only way, I can think of, to accomplish that is, to wrap the request, catch calls to ServletRequest.getRequestDispatcher() and return a wrapped instance of RequestDispatcher, that informs me, when it's include-method is called. But since Servlet 3.0 there are also the methods ServletContext.getRequestDispatcher() and ServletContext.getNamedDispatcher(). I cannot catch that methods, because my filter cannot wrap the ServletContext.

Is there any other way to detect calls to RequestDispatcher.include()?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kai Moritz
  • 611
  • 2
  • 7
  • 19

1 Answers1

1

adding

<dispatcher>INCLUDE</dispatcher>

to filter-mapping seems to work refer

Community
  • 1
  • 1
yakamoto
  • 50
  • 10
  • Yes, I too stumbled accross this possibility today. But I am not sure, if it helps, because in that case the filter is called, when the include is already in progress and, hence, the container might already ignore calls to ServletResponse.addHeader(), as called for in the specification. I will try that way and report back, if it helps! – Kai Moritz Nov 14 '12 at 12:28
  • I have tried it. As expected, I cannot add headers from a filter, that was mapped with `INCLUDE`, because an include cannot alter the headers of the response... – Kai Moritz Nov 14 '12 at 22:38