Way2Java

autoFlush JSP page directive with Example

One of the advantages with JSP is that Designers gave many handles to control the behavior of container. One such one is autoFlush JSP attribute of page directive.
1. What is autoFlush JSP?
2. How autoFlush JSP works?
3. What is the syntax of autoFlush JSP?

Syntax: <%@ page autoFlush="true/false" %>

Example 1: When autoFlush is set to true.

<%@ page autoFlush="true" %>

Example 2: When autoFlush is set to false.

<%@ page autoFlush="false" %>

4. What are the salient points with autoFlush and buffer attributes of page directive?

  1. What is the performance aspect of buffer size?

    Any response data is stored first in a buffer memory on server-side before sending to client. If the buffer size is set to less, many number of times the data is flushed to client. The client’s browser renders fast.

  2. What could be optimum size of buffer that can be set?

    Every communication internally uses sockets, even though Programmer does not write or mention anywhere in his coding. Always keep buffer size a little higher than underlying socket buffer size. If set less, even though the buffer is full, it will not be flushed out to client really. Optimum size depends on the application needs whether application sends lots of data or images often.

  3. How to know the default buffer size of my Web server?

    The getBufferSize() method of JspWriter returns the buffer size supported by the Web server like Tomcat, Weblogic etc.

  4. What is autoFlush JSP when buffer is full?

    When autoFlush attribute of page directive is set to true, the buffer gets automatically flushed out when gets filled. It is of no problem and best practice also. If autoFlush is set to false, the Programmer should take care himself not to get the buffer filled up completely and flush himself. In case buffer is full and before Programmer did not flush explicitly, container raises exception.

Syntax of setting both buffer and autoflush JSP.

<%@ page buffer="16kb" autoFlush="false"%>