Reputation: 11
I want to set HTTP headers depending on the type of response. My problem is that some reponses commit their headers before I can set mine. Any suggestions about how to handle this?
Upvotes: 1
Views: 1332
Reputation: 718698
One idea is to set the response header in the response object in a filter before the servlet is called.
Another one is to provide the servlet with a response wrapper that prevents the real response from actually being committed. This might entail buffering the response body in memory.
But I think that the best approach is to modify the servlets so that they set the required headers.
Do I HAVE to buffer the entire response by providing my own outputstream to the wrapper ? Can I overwrite flush() or something like that ?
It is up to you how you implement it. The constraint is that response wrapper object must somehow arrange that the special header gets added before the writing of the response body starts. The conceptually simple way is to buffer the response body, but you could also get the response wrapper to provide an output stream / writer that adds the header before the first flush to the real response output stream / writer.
I am worried about buffering causing performance issues
Yes, well it could do, though you would need to look at the entire webapp to determine if this is significant.
From a performance perspective, the best solution is to get the servlet to set the header itself.
Upvotes: 1