Share on Google+Share on Google+

Java ServletResponse Interface

In this tutorial, we will discuss about ServletResponse Interface.

Java ServletResponse Interface

In this tutorial, we will discuss about ServletResponse Interface.

javax.servlet.ServletResponse Interface :

It is defined in javax.servlet package. Packages that use ServletResponse are javax.servlet and javax.servlet.http. ServletResponse interface defines object to send response to the client.object of ServletResponse is created by the servlet container and works as argument for the servlet's service method. If sending binary data in MIME body response then you have to use ServletOutputStream returned by getOutputStream(). For Character data, use PrintWriter object returned by getWriter().

It provides many methods. Here, we are defining some of them.

  • getBufferSize() : It returns the actual buffer size which is used for the response. It returns 0 if no buffering is used.
  • getOutputStream() : This method returns a ServletOutputStream suitable for writing binary data in the response.
  • getWriter() : It returns PrintWriter object for writing formatted text responses.

Other methods are - flushBuffer(), getCharacterEncoding(), getLocale(), isCommitted(), reset(), setBufferSize(int size), setContentLength(int len),
setContentType(java.lang.String type), setLocale(java.util.Locale loc) .

Example : -

package net.roseindia;

import javax.servlet.*;
import javax.servlet.http.*;

public class ServletResponseExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<h1>ServletResponse Example</h1>");

web.xml -

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

<!-- servlet response mapping-->


Output :


Posted on: October 5, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null