Tuesday, 19 August 2014

resetBuffer()

public void resetBuffer()
resetBuffer method clears content in the buffer if the response is not committed without clearing the headers and status code.

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");
            
            out.println("I am not going to print");
            res.resetBuffer();
            out.println("I will print");
        }
    }
}

Output







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment