I've been trying to figure this out for more than a day now. Looked all over the internet and used multiple references as examples.
I use a custom HttpServletResponseWrapper, and I override the getOutputStream() method. I'm essentially following this example: How to read and copy the HTTP servlet response output stream content for logging
Which is very similar, to other examples I used. But the result is the same. I'm successfully logging the response, but my JSP page that I'm redirected to is showing up empty.
Perhaps I'm not understanding some of the fundamentals? Any help whatsoever will be appreciated!
Thank you!
Below is the code I'm using from the example. The HTTPServeletResponseCopier adn ServletOutputStreamCopier can be found here: https://github.com/ukwa/interject/tree/master/interject-servlet-filter/src/main/java/uk/bl/wa/interject/filter
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
if (response.getCharacterEncoding() == null) {
response.setCharacterEncoding("UTF-8");
}
HttpServletResponseCopier responseCopier = new HttpServletResponseCopier((HttpServletResponse) response);
try
{
chain.doFilter(request, responseCopier);
responseCopier.flushBuffer();
}
catch (Exception e)
{
System.out.println("EXCEPTION ECNOUTERED - " + e.getMessage());
e.printStackTrace();
}
finally
{
byte[] copy = responseCopier.getCopy();
System.out.println("Logging.....");
System.out.println(new String(copy, response.getCharacterEncoding())); // Do your logging job here. This is just a basic example.
System.out.println("Logging.....");
}
}
Edit: I think I know what I'm doing wrong, but I don't know how to fix it.
Apparently, when dealing with a RESPONSE fIlter, you need to handle the response AFTER the filter-chain. Which is why my response is empty. What I tried doing was
"String responseString = new String(copy);
response.getWriter().write(responseString);"
But this seems like a quick and easy work around that's not ACTUALLY solving the problem. Any thoughts?