ServletResponse Interface – Servlets

The ServletResponse represents the response which is sent back to the user. The servlet container creates a ServletResponse object and passes it as an argument to the servlet’s service method.

Important Methods of ServletResponse Interface

  • public PrintWriter getWriter() : This method returns a PrintWriter object that can send character text to the client.
  • public ServletOutputStream getOutputStream() : This method returns a ServletOutputStream suitable for writing binary data in the response.
  • public String getCharacterEncoding() : This method returns the name of the MIME charset used.
  • public void setContentType(String type) : Sets the content type of the response being sent as response.
  • public int getBufferSize() : This method returns the actual buffer size used for the response. If no buffering is used, this method returns 0.
  • public void flushBuffer() : Forces any content in the buffer to be written to the client.
  • public void setLocale(Locale loc) : This method sets the locale of the response, if the response has not been committed yet.
  • public boolean isCommitted(): This method returns a boolean indicating if the response has been committed.
  • public void reset() : Clears the data of the buffer along with the headers and status code.

package com.javainterviewpoint;


import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ServletResponseInterfaceExample implements Servlet
    public void init(ServletConfig config) throws ServletException {
    public void destroy() {
    public void service(ServletRequest request, ServletResponse
    response) throws ServletException, IOException 
        //Getting the PrintWriter object
        PrintWriter out = response.getWriter();
        //Setting the Content type
        out.println("Content Type : "+request.getContentType());
        //Posting the response back
        String name = request.getParameter("name");
        out.println("<br>Welcome \""+name+"\"");
    public String getServletInfo() {
    return null;
    public ServletConfig getServletConfig() {
    return null;


 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" 
id="WebApp_ID" version="2.5">


 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <form action="./ServletResponseInterfaceExample" method = "POST">
            Name : <input type="text" name="name"><br>
            <input type="submit" value="Submit Form">
            <input type="reset" value="Reset Form">


Hit on the Url : http://localhost:8080/ServletTutorial/index.jsp







Hit on the Submit button

Content Type : application/x-www-form-urlencoded
Welcome “JavaInterviewPoint”

Leave a Reply

Your email address will not be published. Required fields are marked *