Home Tutorial Servlet Java ServletResponse Interface


Share on Google+Share on Google+
Java ServletResponse Interface
Posted on: October 5, 2012 at 12:00 AM
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 :

ServletResponseExample.java -

package net.roseindia;

import java.io.*;
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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<!-- servlet response mapping-->


Output :


Related Tags for Java ServletResponse Interface:

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.

Posted on: October 5, 2012

Recommend the tutorial

Advertisements Advertisements