Home

HttpServletResponse

HttpServletResponse is an interface in the Java Servlet API that represents the HTTP response to be sent to a client. It is provided to servlet methods such as doGet and doPost by the servlet container, which uses it to construct and transmit the response. Implementations are supplied by the web server or container and control how the response is delivered.

The interface exposes methods to control status codes, headers, and the response body. Key operations include

Additional controls include buffering via setBufferSize(int), flushBuffer(), and resetBuffer(); and state management with isCommitted() and reset().

HttpServletResponse is part of javax.servlet.http in older specifications and jakarta.servlet.http in newer ones; the exact package

setStatus(int
sc)
and
sendError(int
sc)
for
signaling
status
or
errors;
sendRedirect(String
location)
for
client
redirection.
Headers
can
be
set
or
added
with
setHeader(String
name,
String
value)
and
addHeader(String
name,
String
value).
Content
type
and
encoding
are
configured
with
setContentType(String)
and
setCharacterEncoding(String).
The
response
body
can
be
produced
via
getOutputStream()
for
binary
data
or
getWriter()
for
character
data.
Content
length
can
be
specified
with
setContentLength(int)
or
setContentLengthLong(long).
Cookies
can
be
added
with
addCookie(Cookie).
Date
and
integer
headers
can
be
set
with
setDateHeader
and
setIntHeader.
The
interface
also
supports
setLocale(Locale)
to
influence
localization
of
the
response.
depends
on
the
servlet
API
version
used.
In
practice,
the
response
object
allows
a
servlet
to
construct
the
HTTP
response
before
the
data
is
returned
to
the
client,
with
the
container
handling
the
details
of
network
transmission.