autoFlush JSP page directive with Example

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?
  • autoFlush is one of the 14 attributes, JSP supports.
  • It takes boolean values of true or false.
  • It indicates the container to flush the data or not when the buffer gets filled to be sent to client.
2. How autoFlush JSP works?
  • If not set by the Programmer, the default value is true, indicating flushing is required.
  • When set to false, the autoFlush raises exception when the buffer is full.
3. What is the syntax of autoFlush JSP?

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

Example 1: When autoFlush is set to true.

<%@ page autoFlush="true" %>

  • It is the default value.
  • When the buffer is full, the data is flushed out to the output stream of the response object to send to client.
  • This is done by the container implicitly.

Example 2: When autoFlush is set to false.

<%@ page autoFlush="false" %>

  • The container raises exception when the autoFlush is set to false, when buffer gets filled up.

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"%>

Leave a Reply

Your email address will not be published. Required fields are marked *