gpt4 book ai didi

jsf - 如何将响应大小和时间插入页面本身,至少部分?

转载 作者:行者123 更新时间:2023-12-03 23:23:57 25 4
gpt4 key购买 nike

我意识到这是一个先有鸡还是先有蛋的问题,并且不可能准确地解决呈现页面所花费的时间(或响应的大小)并将该数字插入页面本身而不影响任何一项措施。不过,我正在寻找一种方法将任一数字部分插入 JSF/Facelets/Seam 应用程序的页面中。

例如,在 .jsf 页面的底部某处:

<!-- page size: 10.3Kb -->
<!-- render time: 0.2s -->

我遇到过 JSFUnit 的 JSFTimer ,真的很方便。但是,阶段监听器方法不允许将 RENDER_RESPONSE 阶段的结果插入到页面中。也不知道如何访问到目前为止编码的响应的大小。

是否有一种快速而肮脏的方法可以在 RENDER_RESPONSE 结束时或之后连接到某种后处理事件,并将两个数字都注入(inject)到即将呈现的页面中?解决这个问题的一种方法可能是通过 servlet 过滤器,但我正在寻找更简单的方法;也许是 Seam 或 Facelets 的一个技巧......

谢谢,
-一种

最佳答案

这是 Apache Commons IO 的完美用例 CountingOutputStream .您需要创建一个 Filter它使用 HttpServletResponseWrapper 替换 OutputStream用这个响应并替换 Writer以及应该包裹包裹的OutputStream .然后获取 HttpServletResponseWrapper请求范围内的实例,以便您可以获得 getByteCount()来自 CountingOutputStream .

这是 CountingFilter 的启动示例:

public class CountingFilter implements Filter {

@Override
public void init(FilterConfig arg0) throws ServletException {
// NOOP.
}

@Override
public void doFilter(ServletRequest request, final ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpres = (HttpServletResponse) response;
CountingServletResponse counter = new CountingServletResponse(httpres);
HttpServletRequest httpreq = (HttpServletRequest) request;
httpreq.setAttribute("counter", counter);
chain.doFilter(request, counter);
counter.flushBuffer(); // Push the last bits containing HTML comment.
}

@Override
public void destroy() {
// NOOP.
}

}
CountingServletResponse :
public class CountingServletResponse extends HttpServletResponseWrapper {

private final long startTime;
private final CountingServletOutputStream output;
private final PrintWriter writer;

public CountingServletResponse(HttpServletResponse response) throws IOException {
super(response);
startTime = System.nanoTime();
output = new CountingServletOutputStream(response.getOutputStream());
writer = new PrintWriter(output, true);
}

@Override
public ServletOutputStream getOutputStream() throws IOException {
return output;
}

@Override
public PrintWriter getWriter() throws IOException {
return writer;
}

@Override
public void flushBuffer() throws IOException {
writer.flush();
}

public long getElapsedTime() {
return System.nanoTime() - startTime;
}

public long getByteCount() throws IOException {
flushBuffer(); // Ensure that all bytes are written at this point.
return output.getByteCount();
}

}
CountingServletOutputStream :
public class CountingServletOutputStream extends ServletOutputStream {

private final CountingOutputStream output;

public CountingServletOutputStream(ServletOutputStream output) {
this.output = new CountingOutputStream(output);
}

@Override
public void write(int b) throws IOException {
output.write(b);
}

@Override
public void flush() throws IOException {
output.flush();
}

public long getByteCount() {
return output.getByteCount();
}

}

您可以在任何(甚至非 JSF)页面中使用它,如下所示:
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Counting demo</title>
</h:head>
<h:body>
<h1>Hello World</h1>
</h:body>
</html>
<!-- page size: #{counter.byteCount / 1000}KB -->
<!-- render time: #{counter.elapsedTime / 1000000}ms -->

关于jsf - 如何将响应大小和时间插入页面本身,至少部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3220820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com