buffer JSP page directive with Example

buffer JSP page directive with Example

1. What is buffer JSP?
  1. JSP first writes the response data to the buffer and when the buffer is full, the data is sent to client.
  2. "buffer" is one of the 14 attributes of page directive.
  3. When the PrintWriter object is closed with out.close(), the unfilled buffer is flushed to the client.
2. What is the default buffer size in JSP?
  1. Using this page directive attribute, the buffer size can be increased or decreased or set to none (means no buffer is maintained).
  2. If not set by the Programmer, the default buffer size is 8 KB.
3. What is the syntax and examples of writing buffer JSP?

Syntax: <%@ page buffer= "integer/none" %>

Example 1: buffer size set to 5 KB with buffer attribute of page directive

The buffer attribute of page directive may also be used to change the buffer size. The example below sets the buffer size to 4 KBytes.

<%@ page buffer="5kb" %>

The response is copied into the 5 KB buffer before sending to client.

Example 2: To disable the buffer

<%@ page buffer="none" %>

Here, buffer is not maintained or set to zero. The response data is immediately written to outputstream of client. This kills performance and increases network traffic.

4. What are the salient points with autoFlush and buffer JSP 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 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.

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

Leave a Reply

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