Saturday 14 March 2015

jsp Buffering the response


'page' directive provides two attributes 'buffer' and 'autoFlush' which Specifies the buffering model for the initial out JspWriter to handle content output from the page.

buffer
The value for this attribute must specified in kilobytes(kb). You can set 'buffer' to 'none' also. If buffer sets to none, then there is no buffering and all output is written directly through to the ServletResponse PrintWriter.

autoFlush
Specifies whether the buffered output should be flushed automatically (true value) when the buffer is filled, or whether an exception should be raised (false value) to indicate buffer overflow. By default 'autoFlush' set to true.

Lets see the different combinations
buffer autoFlush Description
none true.
Data written to the servlet response output stream
as soon as they are generated.
none false. It is illegal, resulting in a translation error, to set autoFlush to false when buffer=none.
10kb true. Whenever the buffer filled, it flushes the data automatically.
10kb false. Whenever the buffer filled, an exception raised to indicate bufferflow.


<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.Random, java.io.*" %>
<%@page buffer ="10kb" autoFlush="false"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
        <h1> 
            <%
                for(int i=0; i<9999999; i++){
                    out.println("i");
                }
            %>

        </h1>
        
    </body>
</html>


Above jsp sets the buffer size to '10kb' and autoFlush to false. So whenever the buffer fills, it throws exception to indicate Buffer overflow.

Sample Output






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment