I think the response is getting committed when it reaches ServletFilter A
at Step 4
. Once the response is committed, i.e, headers are written to the client, you cannot do operations which requires adding headers. The operations like adding cookies.
If you want the response not to be committed till Step 4
try wrapping HttpServletResponse
and returning your custom output stream which buffers the data till it reaches step 4
and then commits the response.
Here is the sample code :
public class ResponseBufferFilter implements Filter
{
public void init(FilterConfig filterConfig) throws ServletException
{
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException
{
HttpServletResponse httpResponse = (HttpServletResponse)response;
BufferResponseWrapper wrapper = new BufferResponseWrapper(httpResponse);
filterChain.doFilter(request, resposneWrapper);
response.getOutputStream().write(wrapper .getWrapperBytes());
}
public void destroy()
{
}
private final class BufferResponseWrapper extends HttpServletResponseWrapper
{
MyServletOutputStream stream = new MyServletOutputStream();
public BufferResponseWrapper(HttpServletResponse httpServletResponse)
{
super(httpServletResponse);
}
public ServletOutputStream getOutputStream() throws IOException
{
return stream;
}
public PrintWriter getWriter() throws IOException
{
return new PrintWriter(stream);
}
public byte[] getWrapperBytes()
{
return stream.getBytes();
}
}
private final class MyServletOutputStream extends ServletOutputStream
{
private ByteArrayOutputStream out = new ByteArrayOutputStream();
public void write(int b) throws IOException
{
out.write(b);
}
public byte[] getBytes()
{
return out.toByteArray();
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…