esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

What are the buffer and autoFlush attributes in the 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:

 
This code sample is only viewable to esus.com members
Login or become a member!


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:

This code sample is only viewable to esus.com members
Login or become a member!


Setting buffer=none and autoFlush=false


Can't do that!

AutoFlushFalse.jsp:

This code sample is only viewable to esus.com members
Login or become a member!


The following exception will be thrown:

 
This code sample is only viewable to esus.com members
Login or become a member!


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:

This code sample is only viewable to esus.com members
Login or become a member!


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:

This code sample is only viewable to esus.com members
Login or become a member!


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:

This code sample is only viewable to esus.com members
Login or become a member!


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


Further Information
Author of answer: Joris Van den Bogaert

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.