响应正在提交并且doFilter链被破坏 [英] Response is committing and doFilter chain is broken

查看:139
本文介绍了响应正在提交并且doFilter链被破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我需要发生的事情:

In order this is what I need to happen:

请求 blah.com/test


  1. ServletFilter A - 创建个人资料,然后调用 chain.doFilter

  2. ServletFilter B(跳过,因为url模式不匹配)

  3. Servlet - 改变配置文件, repsonse.setStatus & response.addHeader(Location,target)

  4. ServletFilter A - 应该根据个人资料创建一个cookie

  1. ServletFilter A - creates profile, then calls chain.doFilter
  2. ServletFilter B (is skipped because url pattern doesn't match)
  3. Servlet - alters the profile, repsonse.setStatus & response.addHeader("Location", target)
  4. ServletFilter A - should create a cookie based on the profile

实际发生的事情:


  1. ServletFilter A - 创建个人资料,然后调用 chain.doFilter

  2. ServletFilter B(跳过因为url模式不匹配)

  3. Servlet - 改变个人资料, repsonse.setStatus & response.addHeader(Location,target)

  4. 重定向已提交且ServletFilter A未完成任务

  1. ServletFilter A - creates profile, then calls chain.doFilter
  2. ServletFilter B (is skipped because url pattern doesn't match)
  3. Servlet - alters the profile, repsonse.setStatus & response.addHeader("Location", target)
  4. Redirect is committed and ServletFilter A does not complete tasks

我认为这可能与您在ServletFilter配置中设置的调度程序值有关。

I think this may be related to the dispatcher values that you can set in the ServletFilter configs.

任何想法?

推荐答案

我认为当响应达到 ServletFilter A at 步骤4 。提交响应后,即将标头写入客户端,您无法执行需要添加标头的操作。添加cookie等操作。

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.

如果您希望在步骤4 之前不提交响应,请尝试包装 HttpServletResponse 并返回自定义输出流,缓冲数据直到达到步骤4 然后提交响应。

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.

以下是示例代码:

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();
    }

}
}

这篇关于响应正在提交并且doFilter链被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆