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

MethodsDescription
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

MethodsDescription
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:

servlet-response

Following are the steps for creating the program.

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

servlet-response

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.

servlet-response

And write the below code.

	
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>studytonight.com</title>
</head>
<body>
<form align="center" action="display" method="get">
<h3>studytonight.com</h3>
<hr>
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">
</body>
</html>
	

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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>ServletResponse</display-name>
   <servlet>
  	<servlet-name>abc3</servlet-name>
  	<servlet-class>demo4</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>abc3</servlet-name>
  <url-pattern>/display</url-pattern>
  </servlet-mapping>
</web-app>
	

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

servlet-response
servlet-response

Add the below code in the class file.

Demo4.java

	
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class demo4 extends HttpServlet{
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
   {
     res.setContentType("text/html");
     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+"**");
     pwriter.close();
  }
}
	

Now, Run the code

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

servlet-response

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

servlet-response

This is the servlet page.


servlet-response