質問

Trying to mess around with the response content I tried this filter tutorial

So I defined my classes as

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

And then my filter.

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

The filter seems to be ok. I can read "test content" but it seems wrapper.getData() returns 0 bytes. When I call chain.doFilter(..) usually is a servlet that some times dispatches to a jsp page. Tried a URL of a JSP plain page but it does not seem to write data. The filter is configured to catch ALL requests and it does it well.

What can I be doing so wrong? The only difference is that I added @override. But I tried without them also.

Added a 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>
役に立ちましたか?

解決

What fixed the issue was this making pwriter and outpstrm class members (fields).

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

And then changin the methods for geting them

@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;
} 

This seems to be what fixed my problem. Since this change the wrapper worked fine. By the way. Who said that you can't call the method forward(req, res) method of the RequestDispatcher class?.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top