Skip to content
Related Articles

Related Articles

Servlet – Response

View Discussion
Improve Article
Save Article
  • Last Updated : 22 Feb, 2022

A web application is built using Servlet technology (resides at the server-side and generates a dynamic web page). Because of the Java programming language, servlet technology is dependable and scalable. CGI (Common Gateway Interface) scripting language was widely used as a server-side programming language prior to Servlet.

Servlet – Response

A servlet can use this object to help it provide a response to the client. A ServletResponse object is created by the servlet container and passed as an argument to the servlet’s service function.

Use the ServletOutputStream supplied by getOutputStream to deliver binary data in a MIME body response (). Use the PrintWriter object given by getWriter to deliver character data (). Use a ServletOutputStream and manually control the character sections to blend binary and text data, for example, to generate a multipart response.

The setCharacterEncoding(java.lang.String) and setContentType(java.lang.String) methods can be used to provide the charset for the MIME body response, or the setLocale(java.util.Locale) method can be used to specify it implicitly. Implicit requirements are overridden by explicit specifications. ISO-8859-1 will be used if no charset is supplied. For the character encoding to be utilized, the setCharacterEncoding, setContentType, or setLocale methods must be called before getWriter and before committing the response.

Some Important Methods of ServletResponse



String getCharacterEncoding() It returns the name of the MIME charset that was used in the body of the client response.
String getContentType() It returns the response content type. e.g. text, HTML etc.
 ServletOutputStream getOutputStream() This method returns a ServletOutputStream that may be used to write binary data to the response.
PrintWriter getWriter() The PrintWriter object is used to transmit character text to the client.
void setContentLength(int len) Sets the length of the response’s content body. This function sets the HTTP Content-Length header in HTTP servlets.
void setContentType(String type) Sets the type of the response data.
void setBufferSize(int size) specifies the recommended buffer size for the response’s body.
int getBufferSize() Returns the buffer size
void flushBuffer() Any material in the buffer will be forced to be written to the client.
boolean isCommitted() If the response has been committed, this method returns a boolean.
void setLocale(Locale loc) If the answer hasn’t been committed yet, it sets the response’s location.
void reset() Clears the buffer’s data, as well as the headers and status code. To acquire a comprehensive list of ways, go here.

Implementation: The setContentType() and getWriter() methods of the ServletResponse interface were utilised in the example below.

A. File: index.html


<title> GEEKSFORGEEKS </title>
<form action="GFG" method="get">
 Enter your username: 
<input type="text" name="uname">
<input type="submit" value="login">

B. Application File (


import javax.servlet.http.*;
import javax.servlet.*;
public class GFG extends HttpServlet{
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
     PrintWriter pwriter=res.getWriter();
     String name=req.getParameter("uname");
     pwriter.println("This is user details page:");
     pwriter.println("Hello "+name);

C. web.xml




First Screen shows the following output:

Second Screen shows the following output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!