HttpServletRequest and HttpServletResponse in Servlets

There are four main entities involved in the HTTP request-response model:

  1. Client
  2. Web container
  3. Request
  4. Response

We will learn about request and response here.


Container provides us with two objects corresponding to request and response, encapsulating the request details and an empty response object when a request is received. These object interfaces also contain many convenience methods. These are passed through the service method of the servlet, which will then pass to us through the doXXX methods.


Interfaces for request and response objects

Two important interfaces related to request are:

  1. javax.servlet.ServletRequest

    • ​​Is protocol independent

  2. javax.servlet.http.HttpServletRequest

    • ​Is specific to an HTTP protocol.

    • HttpServletRequest extends ServletRequest

    • Provides additional useful methods related HTTP requests.


Two important interfaces related to response are:

  1. javax.servlet.ServletResponse

    • Is protocol independent.

  2. javax.servlet.http.HttpServletResponse

    • Is specific to an HTTP protocol.

    • HttpServletResponse extends ServletResponse.

    • Additional convenience methods provide support for:

      • Cookie manipulation .

      • HTTP headers and status codes

      • URL rewriting used for session management


Request Attributes and Parameters

Request attributes are only visible between components involved in the current request, and are destroyed once the request is fulfilled and response is sent. They are stored in ServletRequest implementation object. 

Request parameters are info sent from client apart from standard protocol headers and are read-only from the application. In HTTP, parameters are sent through the URL in the query string (GET) or in the request body (POST).


Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within!!!