Monday, 18 August 2014

setBufferSize (int size)

public void setBufferSize(int size)
Servlet can request buffer using setBufferSize method. The servlet container will use a buffer at least as large as the size requested.

A larger buffer allows more content to be written before anything is actually sent, thus providing the servlet with more time to set appropriate status codes and headers. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.

With out setting the buffer
main.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="get" action="/servlet/GetData">
            <input type="submit" value ="Get Data">
        </form>
    </body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>GetData</servlet-name>
        <servlet-class>GetData</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GetData</servlet-name>
        <url-pattern>/GetData</url-pattern>
    </servlet-mapping>
</web-app>

GetData.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetData extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       try(PrintWriter out = res.getWriter()){
           long time1 = System.currentTimeMillis();
           res.setContentType("text");
           for(int i=0; i<1000000; i++){
               out.print(i);
           }
           long time2 = System.currentTimeMillis();
           out.println("\nTotal time taken is " + (time2-time1) +" MilliSeconds");
       }
       
    }
}

Output

setting the buffer to 1024
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetData extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       try(PrintWriter out = res.getWriter()){
           res.setBufferSize(1024);
           long time1 = System.currentTimeMillis();
           res.setContentType("text");
           for(int i=0; i<1000000; i++){
               out.print(i);
           }
           long time2 = System.currentTimeMillis();
           out.println("\nTotal time taken is " + (time2-time1) +" MilliSeconds");
       }
       
    }
}

Output


As you observe the outputs, 'Total time taken is 2242 MilliSeconds' without setting the buffer and 'Total time taken is 1278 MilliSeconds' by setting the buffer.

Points to remember
1. This method must be called before any response body content is written; if content has been written or the response object has been committed, this method throws an IllegalStateException.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetData extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       PrintWriter out = res.getWriter();
       out.println("Hi");
       res.setBufferSize(1024);
       long time1 = System.currentTimeMillis();
       res.setContentType("text");
       for(int i=0; i<1000000; i++){
           out.print(i);
        }
        long time2 = System.currentTimeMillis();
        out.println("\nTotal time taken is " + (time2-time1) +" MilliSeconds");
    }
}

Try to run the above program, you can observe the 'IllegalStateException'.






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment