The “buffer” and “autoFlush” attributes in the JSP page directive

Anything generated by the JSP page is stored in a buffer. When the buffer is full, it’s sent back to the client (browser). Sometimes, it is necessary to tweak the buffer and autoFlush attributes to give you more control over how and when the buffer is flushed to the browser.

For example, if you’re not careful about a response.sendRedirect, you may get an java.lang.IllegalStateException. This occurs when you’re performing a redirect after data has already been committed to the browser.

The autoFlush attribute tells the JSP engine when the buffer should be flushed. By default it is set to true. If you set it to false and the buffer becomes full, an exception will be thrown.

When the buffer is flushed once, redirection or forwarding won’t work. All changes to the HTTP response header must occur the first time a buffer is sent to the client. For example, be careful where you do cookie processing!

Examples:

Redirecting when buffer not flushed


By default the buffer is 8kb big. The following example tries asks the browser to redirect to another JSP page. Less than 8kb of bytes were written, so the buffer was not flushed. Redirection succeeds.

DefaultBufferNotFlushed.jsp:

<html>
<body>
<% for (int i=0; i<7000; i++) { %>0<% } %>

<% 
   try {
      response.sendRedirect("/docs/ResultingPage.jsp");
   }
   catch(Exception e) {
      e.printStackTrace();
   }
%>
</body>
</html>

Redirecting when buffer already flushed


By default, the buffer is 8kb big. The following examples writes more than 8kb of data and redirectiong will fail. A java.lang.IllegalStateException will be thrown.

DefaultBufferFlushed.jsp:

<html>
<body>
<% for (int i=0; i<9000; i++) { %>0<% } %>

<% 
   try {
      response.sendRedirect("/docs/ResultingPage.jsp");
   }
   catch(Exception e) {
      e.printStackTrace();
   }
%>
</body>
</html>

Setting buffer=none and autoFlush=false


Can’t do that!

AutoFlushFalse.jsp:

<%@ page buffer="none" autoFlush="false" %>
<html>
<body>
<% for (int i=0; i<9000; i++) { %>0<% } %>

<%
   try {
      response.sendRedirect("/docs/ResultingPage.jsp");
   }
   catch(Exception e) {
      e.printStackTrace();
   }
%>
</body>
</html>

The following exception will be thrown:

   Page directive: Illegal combination of buffer="none" && autoFlush="false"

Setting large buffer and default autoFlush (true)


In the following example, the buffer is set to 50kb. Even though lots of data is written, the buffer doesn’t get filled up before doing a redirect. It succeeds.

LargeBuffer.jsp:

<%@ page buffer="50kb" %>
<html>
<body>
<% for (int i=0; i<40000; i++) { %>0<% } %>

<%
   try {
      response.sendRedirect("/docs/ResultingPage.jsp");
   }
   catch(Exception e) {
      e.printStackTrace();
   }
%>
</body>
</html>

Setting autoFlush to false and filling up the buffer


The default buffer is 8kb. The following example sets autoFlush to false and writes more than 8kb to the buffer. The result is a java.io.IOException: Error: JSP Buffer overflow.

Overflow.jsp:

<%@ page autoFlush="false" %>
<html>
<body>
<% for (int i=0; i<9000; i++) { %>0<% } %>

<%
   try {
      response.sendRedirect("/docs/ResultingPage.jsp");
   }
   catch(Exception e) {
      e.printStackTrace();
   }
%>
</body>
</html>

Setting autoFlush to false and manually flushing


This example sets the autoFlush attribute to false and manually flushes the buffer every so often. No overflow exception is thrown, but the redirect won’t work because the buffer has been flushed.

ManualFlushing.jsp:

<%@ page autoFlush="false" %>
<html>
<body>
<% for (int i=0; i<9000; i++) { %>
0
<% 
      if (i % 1000 == 0) {
         out.flush();
      }
   } 
%>

<%
   try {
      response.sendRedirect("/docs/ResultingPage.jsp");
   }
   catch(Exception e) {
      e.printStackTrace();
   }
%>
</body>
</html>

Check out this link for the tag syntax:
http://java.sun.com/products/jsp/tags/11/syntaxref11.fm7.html