Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 925611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:32:52+00:00 2026-05-15T19:32:52+00:00

I realize it’s a chicken and egg problem and that it’s not possible to

  • 0

I realize it’s a chicken and egg problem and that it’s not possible to accurately resolve the time it took to render a page (or the size of response) and insert that number into the page itself without affecting either measure. Nevertheless, I’m looking for a way to insert either number partially in a page of a JSF/Facelets/Seam application.

E.g., at the bottom of a .jsf page somewhere:

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

I’ve come across JSFUnit’s JSFTimer, which is really handy. However, the phase listener approach doesn’t allow the results of RENDER_RESPONSE phase to be inserted into the page. Not sure how to access the size of the response encoded so far either.

Is there a quick and dirty way to hook up to some sort of post-processing event at or after the end of RENDER_RESPONSE and to inject both numbers into the page about to be rendered? One way of approaching this is perhaps through servlet filters, but I’m looking for something simpler; perhaps a trick with Seam or Facelets…

Thanks,
-A

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:32:52+00:00Added an answer on May 15, 2026 at 7:32 pm

    This is a perfect use case for the Apache Commons IO CountingOutputStream. You need to create a Filter which uses HttpServletResponseWrapper to replace the OutputStream of the response with this one and replaces the Writer as well which should wrap the wrapped OutputStream. Then get hold of the HttpServletResponseWrapper instance in the request scope so that you can get the getByteCount() from the CountingOutputStream.

    Here’s a kickoff example of the 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.
        }
    
    }
    

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

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

    You can use it in any (even non-JSF) page as follows:

    <!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 -->
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.