ServletResponse is used to send responses from the servlet back to the client browser. It provides methods to set content type, character encoding, buffering, and send text or binary data using PrintWriter or ServletOutputStream.
- Sends dynamic response data from servlet to client.
- Supports both text and binary output responses.
- Allows configuration of content type and response settings.
Working Flow of Servlet Response
The below diagram illustrates how a client request is processed through the web server and servlet program, and how the response is generated and sent back to the browser after interacting with the database.

- The web browser sends an HTTP request to the web server.
- The web server forwards the request to the servlet program.
- The servlet processes the request and interacts with the database if required.
- The database sends the requested data back to the servlet.
- The servlet generates the HTTP response.
- The web server sends the response back to the browser.
Output Methods in ServletResponse
1. ServletOutputStream
Used to send binary data such as images, PDFs, and files.
- Returned using getOutputStream()
- Handles binary response data
2. PrintWriter
Used to send text or HTML content to the client.
- Returned using getWriter()
- Commonly used for web page responses
Methods of ServletResponse
| Method | Description |
|---|---|
getCharacterEncoding() | Returns response character encoding |
getContentType() | Returns response content type |
getOutputStream() | Returns output stream for binary data |
getWriter() | Returns writer for text response |
setContentLength(int len) | Sets response content length |
setContentType(String type) | Sets response content type |
setBufferSize(int size) | Sets buffer size |
getBufferSize() | Returns buffer size |
flushBuffer() | Sends buffered data to the client |
isCommitted() | Checks whether response is committed |
setLocale(Locale loc) | Sets response locale |
reset() | Clears response buffer and headers |
Example of ServletResponse Interface
In this example, the servlet receives a username from the HTML form and displays it in the browser using setContentType() and getWriter() methods.
Step 1: Create the HTML Form
The index.html file contains a form that accepts the username from the user.
<html>
<body>
<title> GEEKSFORGEEKS </title>
<form action="GFG" method="get">
Enter your username:
<br><br>
<input type="text" name="uname">
<br><br>
<input type="submit" value="login">
</form>
</body>
</html>
Step 2: Create the Servlet Class
The servlet receives the username from the request and sends the response back to the browser.
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class GFG extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pwriter=res.getWriter();
String name=req.getParameter("uname");
pwriter.println("This is user details page:");
pwriter.println("Hello "+name);
pwriter.close();
}
}
Step 3: Configure the Servlet
The web.xml file maps the servlet with the URL pattern.
<web-app>
<servlet>
<servlet-name>GFG</servlet-name>
<servlet-class>GFG</servlet-class>
</servlet><servlet-mapping>
<servlet-name>GFG</servlet-name>
<url-pattern>/GFG</url-pattern>
</servlet-mapping>
</web-app>
Step 4: Run the Application
Deploy the project on the server and open the index.html page in the browser by using the url.
http://localhost:8080/ProjectName/index.html
Enter the username and click the login button.

Output:
The servlet processes the request and displays the response on the browser.

Explanation: The browser displays the entered username dynamically using the ServletResponse object.