Monday 18 August 2014

getBufferSize ()

public int getBufferSize()
Returns the actual buffer size used for the response. If no buffering is used, this method returns 0. The servlet can request a preferred buffer size by using the setBufferSize method. The buffer assigned is not required to be the size requested by the servlet, but must be at least as large as the size requested.

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()){
            res.setBufferSize(1024);
            int size = res.getBufferSize();
            res.setContentType("text/html");
            String str1 = "<html><head><title>Buffer size</title>" +
                           "<body><h1> buffer size : " + size +
                           "</h1></body></html>";
            out.println(str1);
        }
    }
}

Output





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment