Thursday 21 August 2014

flushBuffer()

public void flushBuffer()
Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.

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.setContentType("text/html");
            String str1 = "<html><head><title>Buffer size</title>" +
                           "<body><h1>";
            System.out.println("Is Response Committed " + res.isCommitted());
            out.println(str1);
            out.println("Is Response Committed " + res.isCommitted() +"<br />");
            res.flushBuffer();
            out.println("Is Response Committed " + res.isCommitted());
            out.println("</h1></body></html>");
        }
    }
}

Output






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment