HttpServletResponse Interface with Example


The HttpServletResponse interface enables a servlet to formulate an HTTP response to a client. The response object encapsulates all information to be returned from the server to the client. In the HTTP protocol, this information is transmitted from the server to the client either by HTTP headers or the message body of the request.

Check out: Servlet + JSP + JDBC + MySQL Examples

HttpServletResponse interface Overview

The HttpServletResponse interface extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and cookies.
The servlet container creates an HttpServletResponse object and passes it as an argument to the servlet's service methods (doGet, doPost, etc). For instance, here is the signature of the HttpServlet.doGet() method:
protected void doGet(
    HttpServletRequest request,
    HttpServletResponse response)
      throws ServletException, IOException {
}

HttpServletResponse Interface Class Diagram

Below class diagram shows a HttpServletResponse interface extends the ServletResponse interface to provide HTTP-specific functionality in sending a response:

HttpServletResponse Interface Example

In this example, we will demonstrate the usage of important HttpServletResponse interface methods:
package net.javaguides.servlet.tutorial.httpservlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/httpservletresponse")
public class HttpServletResponseExample extends HttpServlet {

    private static final long serialVersionUID = 1 L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // Writing HTML
        PrintWriter printWriter = response.getWriter();

        // Headers
        response.setHeader("Header-Name", "Header Value");

        // Content-Type
        response.setContentType("text/html");

        // Content-Length
        response.setHeader("Content-Length", "31642");

        // Writing Binary Data
        OutputStream outputStream = response.getOutputStream();
        outputStream.write(123);

        //Redirecting to a Different URL
        response.sendRedirect("https://javaguides.net");

        printWriter.print("<html>");
        printWriter.print("<body>");
        printWriter.print("<h1>Hello World HttpServlet Class Example</h1>");
        printWriter.print("<a href=\"http://www.javaguides.net\">Java Guides</a>");
        printWriter.print("</body>");
        printWriter.print("</html>");
        printWriter.close();
    }
}

Reference

Servlet 4.0 Basic Examples

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments