Signup/Sign In

Introduction to ServletResponse

Servlet API provides two important interfaces ServletResponse and HttpServletResponse to assist in sending response to client.

Some Important Methods of ServletResponse

PrintWriter getWriter()returns a PrintWriter object that can send character text to the client.
void setBufferSize(int size)Sets the preferred buffer size for the body of the response
void setContentLength(int len) Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header
void setContentType(String type)sets the content type of the response being sent to the client before sending the respond.
void setBufferSize(int size)sets the preferred buffer size for the body of the response.
boolean isCommitted()returns a boolean indicating if the response has been committed
void setLocale(Locale loc) sets the locale of the response, if the response has not been committed yet.

HttpServletResponse Interface

HttpServletResponse interface adds the methods that relate to the HTTP response. It extends the ServletResponse interface. The object of HttpServletResponse is created in servlet container.

servlet response

Some Important Methods of HttpServletResponse

void addCookie(Cookie cookie)adds the specified cookie to the response.
void sendRedirect(String location)Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer
int getStatus()gets the current status code of this response
String getHeader(String name)gets the value of the response header with the given name.
void setHeader(String name, String value)sets a response header with the given name and value
void setStatus(int sc)sets the status code for this response
void sendError(int sc, String msg)sends an error response to the client using the specified status and clears the buffer

Example of HttpServletResponse interface on Eclipse

For creating a HttpServletResponse interface below is the directory structure of the program:


Following are the steps for creating the program.

Step 1: Create a dynamic project on eclipse by clicking on File => New => Dynamic Web Project


Step 2: Now create an HTML file.

Right-click on the project and then click on HTML file. Give the name of the file and then click on the finish button.


And write the below code.

<!DOCTYPE html>
<meta charset="ISO-8859-1">
<form align="center" action="display" method="get">
Enter User name: <input type="text" name="val1"><br><br>
Enter Password: &nbsp;&nbsp;<input type="password" name="val2" ><br><br>
<input type="submit" value="login">

Step 3: now add the below code in web.xml file.

web.xml file is a deployment descripter. Here we have all the configurations.

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

Step 4: Now next create a servlet. For this create a class. Give the package name and the class name.


Add the below code in the class file.

import javax.servlet.http.*;
import javax.servlet.*;
public class demo4 extends HttpServlet{
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
     PrintWriter pwriter=res.getWriter();
     String uname=req.getParameter("val1");
     String pw=req.getParameter("val2");
     pwriter.println("User Details Page:");
     pwriter.println("Hello "+uname);
     pwriter.println("Your Password is **"+pw+"**");

Now, Run the code

To run the code, right-click on the project and select Run As => Run on Server.


Below is the demo4.html page. Fill all the fields and click on the login button for landing in the servlet page.


This is the servlet page.