jump to navigation

The Response Object August 23, 2005

Posted by Coolguy in Servlets/Jsp.
  • 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.
  • A servlet container is allowed, but not required, to buffer output going to the client for efficiency purposes.
  • The following methods in the ServletResponse interface allow a servlet to access and set buffering information:
    • getBufferSize
    • setBufferSize
    • isCommitted
    • reset
    • resetBuffer
    • flushBuffer
  • These methods are provided on the ServletResponse interface to allow buffering operations to be performed whether the servlet is using a ServletOutputStream or a Writer.
  • The getBufferSize method returns the size of the underlying buffer being used. If no buffering is being used, this method must return the int value of 0 (zero).
  • The servlet can request a preferred buffer size by using the setBufferSize method. The buffer assigned is not required to be the size requested by the servlet, but must be at least as large as the size requested.
  • The method must be called before any content is written using a ServletOutputStream or Writer. If any content has been written or the response object has been committed, this method must throw an IllegalStateException.
  • The isCommitted method returns a boolean value indicating whether any response bytes have been returned to the client.
  • The flushBuffer method forces content in the buffer to be written to the client.
  • The reset method clears data in the buffer when the response is not committed. Headers and status codes set by the servlet prior to the reset call must be cleared as well.
  • The resetBuffer method clears content in the buffer if the response is not committed without clearing the headers and status code.
  • If the response is committed and the reset or resetBuffer method is called, an IllegalStateException must be thrown. The response and its associated buffer will be unchanged.
  • A servlet can set headers of an HTTP response via the following methods of the HttpServletResponse interface:
  • The setHeader method sets a header with a given name and value. A previous header is replaced by the new header. Where a set of header values exist for the name, the values are cleared and replaced with the new value.It overwrites a exisiting value if the header is present.
  • The addHeader method adds a header value to the set with a given name. If there are no headers already associated with the name, a new set is created.It adds a additional value to the header if its already present.
  • Headers may contain data that represents an int or a Date object. The following convenience methods of the HttpServletResponse interface allow a servlet to set a header using the correct formatting for the appropriate data type:
  • To be successfully transmitted back to the client, headers must be set before the response is committed. Headers set after the response is committed will be ignored by the servlet container.
  • Servlet programmers are responsible for ensuring that the Content-Type header is appropriately set in the response object for the content the servlet is generating.
  • Servlet containers must not set a default content type when the servlet programmer does not set the type.
  • Containers use the X-Powered-By HTTP header to publish its implementation information. Eg: X-Powered-By: Servlet/2.4 JSP/2.0 (Tomcat/5.0 JRE/1.4.1)
  • The following convenience methods exist in the HttpServletResponse interface:
    • sendError
  • The sendRedirect method will set the appropriate headers and content body to redirect the client to a different URL.
  • It is legal to call this method with a relative URL path, however the underlying container must translate the relative path to a fully qualified URL for transmission back to the client. If a partial URL is given and, for whatever reason, cannot be converted into a valid URL, then this method must throw an IllegalArgumentException.
  • The sendError method will set the appropriate headers and content body for an error message to return to the client. An optional String argument can be provided to the sendError method which can be used in the content body of the error.
  • These methods will have the side effect of committing the response, if it has not already been committed, and terminating it. No further output to the client should be made by the servlet after these methods are called. If data is written to the response after these methods are called, the data is ignored.
  • Servlets should set the locale and the character encoding of a response.
  • The locale is set using the ServletResponse.setLocale method. The method can be called repeatedly; but calls made after the response is committed have no effect.
  • If the servlet does not set the locale before the page is committed, the container’s default locale is used to determine the response’s locale
  • If the servlet does not specify a character encoding before the getWriter method of the ServletResponse interface is called or the response is committed, the default ISO-8859-1 is used.
  • Containers must communicate the locale and the character encoding used for the servlet response’s writer to the client if the protocol in use provides a way for doing so. In the case of HTTP, the locale is communicated via the Content-Language header, the character encoding as part of the Content-Type header for text media types.
  • When a response is closed, the container must immediately flush all remaining content in the response buffer to the client.
  • The following events indicate that the servlet has satisfied the request and that the response object is to be closed:
    • The termination of the service method of the servlet.
    • The amount of content specified in the setContentLength method of the response has been written to the response.
    • The sendError method is called.
    • The sendRedirect method is called.
  • Each response object is valid only within the scope of a servlet’s service method, or within the scope of a filter’s doFilter method. Containers commonly recycle response objects in order to avoid the performance overhead of response object creation.


  • println() to a PrintWriter
  • PrintWriter writer = response.getWriter();
  • write() to a ServletOutputStream
  • ServletOutputStream out=response.getOutputStream();
  • setHeader() takes two string parameters


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: