I simply want to use a servlet filter to take the response from JSPs and append a string before sending it back to the client. My servlet filter is in the chain and executing, but I'm receiving the error "OutputStream already retrieved"
Below is my doFilter method. Please let me know what I'm doing wrong, or if there is a simpler way of doing this. Thank you.
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
if (request.getAttribute(FILTER_APPLIED) == null)
{
request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
String closingTags = "</body></html>";
HttpServletResponseWrapper wrappedResponse =
new HttpServletResponseWrapper((HttpServletResponse)response);
chain.doFilter(request, wrappedResponse);
wrappedResponse.getWriter().append(closingTags);
}
else {
chain.doFilter(request, response);
}
}
Below is my doFilter method. Please let me know what I'm doing wrong, or if there is a simpler way of doing this. Thank you.
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
if (request.getAttribute(FILTER_APPLIED) == null)
{
request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
String closingTags = "</body></html>";
HttpServletResponseWrapper wrappedResponse =
new HttpServletResponseWrapper((HttpServletResponse)response);
chain.doFilter(request, wrappedResponse);
wrappedResponse.getWriter().append(closingTags);
}
else {
chain.doFilter(request, response);
}
}