Http Servlet Request Loses Params from POST Body After Reading Once
In this situation, it's crucial to understand that when dealing with a POST request, the parameters are typically sent in the request body. By default, Servlet containers consume the input character stream or binary stream when the parameters are accessed for the first time. As a result, subsequent attempts to access them in the filter chain fail.
To overcome this issue, there are two primary approaches to consider:
1. Interceptor Components:
As an alternative to using the filter chain, you could utilize interceptor components. These components can be implemented using aspects and allow for the operation of the parsed request body without relying on the consumed request InputStream. This approach is typically more efficient since the request InputStream is only parsed once to create your own model object.
2. HttpServletRequestWrapper:
For cases where the filter chain is preferred at the HTTP layer, extending HttpServletRequestWrapper is an effective method. This technique involves consuming the request InputStream and caching the bytes:
public class MultiReadHttpServletRequest extends HttpServletRequestWrapper { private ByteArrayOutputStream cachedBytes; public MultiReadHttpServletRequest(HttpServletRequest request) { super(request); } @Override public ServletInputStream getInputStream() throws IOException { if (cachedBytes == null) cacheInputStream(); return new CachedServletInputStream(cachedBytes.toByteArray()); } @Override public BufferedReader getReader() throws IOException{ return new BufferedReader(new InputStreamReader(getInputStream())); } private void cacheInputStream() throws IOException { /* Cache the inputstream in order to read it multiple times. For * convenience, I use apache.commons IOUtils */ cachedBytes = new ByteArrayOutputStream(); IOUtils.copy(super.getInputStream(), cachedBytes); } /* An input stream which reads the cached request body */ private static class CachedServletInputStream extends ServletInputStream { private final ByteArrayInputStream buffer; public CachedServletInputStream(byte[] contents) { this.buffer = new ByteArrayInputStream(contents); } @Override public int read() { return buffer.read(); } @Override public boolean isFinished() { return buffer.available() == 0; } @Override public boolean isReady() { return true; } @Override public void setReadListener(ReadListener listener) { throw new RuntimeException("Not implemented"); } } }
By wrapping the original request with MultiReadHttpServletRequest before passing it through the filter chain, the request body can be read multiple times. Note that this solution also facilitates multiple reads through the getParameterXXX methods.
Edit:
For newer versions of the ServletInputStream interface, additional methods like isReady and setReadListener need to be implemented. Refer to this question for more information.
The above is the detailed content of Why Does My Http Servlet Request Lose POST Body Parameters After a Single Read?. For more information, please follow other related articles on the PHP Chinese website!