How to read and copy the HTTP servlet response output stream content for logging

You need to create a Filter wherein you wrap the ServletResponse argument with a custom HttpServletResponseWrapper implementation wherein you override the getOutputStream() and getWriter() to return a custom ServletOutputStream implementation wherein you copy the written byte(s) in the base abstract OutputStream#write(int b) method. Then, you pass the wrapped custom HttpServletResponseWrapper to the FilterChain#doFilter() call instead … Read more

Http Servlet request lose params from POST body after read it once

As an aside, an alternative way to solve this problem is to not use the filter chain and instead build your own interceptor component, perhaps using aspects, which can operate on the parsed request body. It will also likely be more efficient as you are only converting the request InputStream into your own model object … Read more

How to use a servlet filter in Java to change an incoming servlet request url?

Implement javax.servlet.Filter. In doFilter() method, cast the incoming ServletRequest to HttpServletRequest. Use HttpServletRequest#getRequestURI() to grab the path. Use straightforward java.lang.String methods like substring(), split(), concat() and so on to extract the part of interest and compose the new path. Use either ServletRequest#getRequestDispatcher() and then RequestDispatcher#forward() to forward the request/response to the new URL (server-side redirect, … Read more