没有使用filter和ByteArrayOutputStream写入响应



我有以下过滤器:

@WebFilter( filterName = "myfilter" )
public class WebCacheFilter implements Filter {
    @Override
    public void init( FilterConfig filterConfig ) {}
    @EJB
    AppExceptionLogger logger;
    @Override
    public void doFilter( ServletRequest req, ServletResponse res, final FilterChain chain )
    throws IOException, ServletException {
        final HttpServletRequest request = ( HttpServletRequest )req;
        final HttpServletResponse response = ( HttpServletResponse )res;
        execute( new StreamAction<ByteArrayOutputStream>() {
            @Override
            public void doAction( ByteArrayOutputStream stream ) throws Exception {
                chain.doFilter( request, new ProxiedServletResponse( response, stream ) );
                response.getOutputStream()
                    .write( stream.toByteArray() );
            }
        } );
    }
    @Override
    public void destroy() {}
    private void execute( StreamAction<ByteArrayOutputStream> action ) throws IOException {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            action.doAction( stream );
        } catch ( Exception e ) {
            logger.logInternal( e );
        } finally {
            stream.close();
        }
    }
}

在web.xml中被映射为

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <filter-mapping>
        <filter-name>myfilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

ProxiedServletResponse类来自这里:https://github.com/FagnerMartinsBrack/Servlet-GZIP-Compression/blob/95f4bac01e5037ea403f502819d3e74f66ef1ad0/src/main/java/com/fagnerbrack/servlet/gzip/proxy/ProxiedServletResponse.java

test.jsp文件内容如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Página de teste</title>
    <!-- Descrição do sistema -->
    <meta name="description" content="">
    <!-- Ajusta o conteúdo de acordo com o tamanho da tela em aparelhos mobile -->
    <meta name="viewport" content="width=device-width">
  </head>
  <body>
    test page
  </body>
</html>

当我尝试访问webapptest.jsp文件时,浏览器不打印任何东西。似乎response.getOutputStream().write( stream.toByteArray() );部分不工作。为什么呢?

注意:如果你试图访问一个javascript文件,如/static/js/jquery.js它正确加载。只有JSP文件没有加载。

Note2:baos.toByteArray()部分中,JSP文件的ByteArrayOutputStream的长度为0 (!!)

jsp写入getWriter(),而不是getOutputStream()。在getWriter()中,您将ByteArrayInputStream包装在PrintWriter中。PrintWriter有一个8K字符缓冲区。JSP显然小于8K字符。在获得写入的byte[]之前,您永远不会对PrintWriter的缓冲区进行flush(),因此它永远不会写入包装的ByteArrayInputStream

你得把这婊子冲走。替换

chain.doFilter(request, new ProxiedServletResponse(response, stream));
response.getOutputStream().write(stream.toByteArray());

HttpServletResponse proxiedResponse = new ProxiedServletResponse(response, stream);
chain.doFilter(request, proxiedResponse);
proxiedResponse.flushBuffer();
response.getOutputStream().write(stream.toByteArray());

相关内容

  • 没有找到相关文章

最新更新