|
Java™ by example!
|
|
|
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:
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:
Setting buffer=none and autoFlush=false
Can't do that! AutoFlushFalse.jsp:
The following exception will be thrown:
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:
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:
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:
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!
|
|
|
|
|