Servlet过滤器和响应包装器

时间:2014-03-13 03:49:49

标签: java jsp servlets servlet-filters

尝试使用我尝试的响应内容this filter tutorial

所以我将我的课程定义为

public class FilterServletOutputStream extends ServletOutputStream
{
    //=======================================================================
    private DataOutputStream stream; 
    //=======================================================================
    public FilterServletOutputStream(OutputStream output) { 
      stream = new DataOutputStream(output); 
    }
    //=======================================================================
    @Override
    public void write(int b) throws IOException  {
        stream.write(b); 
    }
    //=======================================================================
    @Override
    public void write(byte[] b) throws IOException  { 
        stream.write(b); 
    }
    //=======================================================================
    @Override
    public void write(byte[] b, int off, int len) throws IOException  {
        stream.write(b,off,len); 
    }
    //=======================================================================
}




public class GenericResponseWrapper extends HttpServletResponseWrapper 
{ 
  private ByteArrayOutputStream output;
  private int contentLength;
  private String contentType;
  public GenericResponseWrapper(HttpServletResponse response) { 
    super(response);
    output=new ByteArrayOutputStream();
  } 
  public byte[] getData() { 
    return output.toByteArray(); 
  } 
  @Override
  public ServletOutputStream getOutputStream() 
  { 
    return new FilterServletOutputStream(output); 
  } 

  @Override
  public PrintWriter getWriter() 
  { 
    return new PrintWriter(getOutputStream(),true); 
  } 

  @Override
  public void setContentLength(int length) { 
    this.contentLength = length;
    super.setContentLength(length); 
  } 

  public int getContentLength() { 
    return contentLength; 
  } 
  @Override
  public void setContentType(String type) { 
    this.contentType = type;
    super.setContentType(type); 
  } 
  @Override
  public String getContentType() { 
    return contentType; 
  } 
} 

然后是我的过滤器。

public void  doFilter(ServletRequest request, ServletResponse response, FilterChain   chain) throws IOException, ServletException
{
    //===================================================================
    GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) response);
    chain.doFilter(request, wrapper);
    OutputStream out = response.getOutputStream();
    out.write(wrapper.getData());
    out.write("test content".getBytes());
    out.close();
    //===================================================================
}

过滤器似乎没问题。我可以阅读“测试内容”,但似乎wrapper.getData()返回0个字节。当我调用chain.doFilter(..)时,通常是一个servlet,有时会调度到jsp页面。尝试了JSP普通页面的URL,但它似乎没有写入数据。 过滤器配置为捕获所有请求,并且它做得很好。

我怎么能这么做错?唯一的区别是我添加了@override。但我也试过没有它们。

添加了一个JSP。

  <%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>
  <!DOCTYPE html>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <style>
  <%-- @include file="/static/css/divs.css" --%>
  <%-- @include file="/static/css/links.css" --%>
  </style>
  <title>title</title>
  </head>
  <body style="margin-top: 0px; margin-left: 0px; margin-right: 0px">
  <%--@include file="publicheader.jsp" --%>
  <div class="desktopwebpagecontainer">
  main page.
  </div>
  <%--@include file="publicfooter.jsp" --%>
  </body>
  </html>

1 个答案:

答案 0 :(得分:1)

解决这个问题的原因是制作pwriter和outpstrm类成员(字段)。

private PrintWriter pwriter = null;
private ServletOutputStream outpstrm = null;

然后改变制作它们的方法

@Override
public ServletOutputStream getOutputStream() 
{ 
    if (outpstrm == null) outpstrm = new AppServletOutputStream(output);
    return outpstrm;
} 
@Override
public PrintWriter getWriter() 
{ 
    if (pwriter == null) pwriter = new PrintWriter(getOutputStream(),true); 
    return pwriter;
} 

这似乎解决了我的问题。由于这个改变,包装工作正常。 顺便说说。谁说你不能调用RequestDispatcher类的方法forward(req,res)方法?。