Generating the Server Response: HTTP Response Headers
Format of the
what response headers are good for
Sending incremental updates to the browser
Settiing Response Header From Servlet
This method saves you the
trouble of translating a Java date in milliseconds since 1970 (as
returned by System.currentTimeMillis, Date.getTime, or
Calendar.getTimeInMillis) into a GMT time string.
- setDateHeader(String header, long milliseconds)
This method spares you the
minor inconvenience of converting an int to a String before inserting
it into a header.
- setIntHeader(String header, int headerValue)
HTTP allows multiple occurrences of the same header name, and you sometimes want to add a new
header rather than replace any existing header with the same name. For example, it is quite common to
have multiple Accept and Set-Cookie headers that specify different supported MIME types and different
cookies, respectively. With servlets version 2.1, set- Header, setDateHeader and setIntHeader always add
new headers, so there is no way to “unset” headers that were set earlier (e.g., by an inherited method).
With servlets version 2.2, setHeader, setDateHeader, and setIntHeader replace any existing headers of the
same name, whereas addHeader, addDateHeader, and addIntHeader add a header regardless of whether a
header of that name already exists. If it matters to you whether a specific header has already been set, use
containsHeader to check. Finally, HttpServletResponse also supplies a number of convenience methods for
specifying common headers. These methods are summarized as follows.
This method sets the Content-Type header and is used by the majority of servlets.
This method sets the Content-Length header, which is useful if the browser supports persistent (keep-alive) HTTP connections.
method inserts a cookie into the Set-Cookie header. There is no
corresponding setCookie method, since it is normal to have multiple
the sendRedirect method sets the Location header as well as setting the status
code to 302.
HTTP 1.1 Response Header And Their Meaning
- The Allow header specifies the request method(GET,
POST) that the server supports. The default service method of servlet
automatically generates the header for OPTIONS request.
- This useful header tells the browser or other client
the circumstances in which the response document can safely be cached. It has
following possible values.
- A value of close for
this response headers instruct the browser not to use persistent HTTP
- It lets you request that the browser ask the use to
save the response to disk in a file of the given name. It is used as follows:Content-Disposition:attachment;filename-file-name
- It indicates the way in which the page was encoded
during transmission. The browser should reverse the encoding before deciding
what to do with the document.
- It indicates the number of bytes in the response.
This information is using only if the browser is using persistent HTTP
- Content- Type
- It gives the MIME(Multipurpose Internet Mail
Extension) type of the response document.
- The header stipulates the time at which the content
should be considered out-of-date and thus no longer be cached. A servlet might
use this header that changes relatively frequently.
- Last- Modified
- This very useful header indicates when the document
was last changed. The client can then cache the document and supply a date by
If-Modified-Since request header in later request.
- This header, which should be included with all
responses that have a status code in the 300s, notifies the browser of the
- Supplying this header with a value of no-cache
instructs HTTP 1.0 clients not to cache the document.
- This header indicates how soon the browser should
ask for an updated page. For example, to tell the browser to ask for a new copy
in 30 seconds, you would specify a value of 30 with
- This header can be used
in conjunction with a 503 response to tell the client how soon it can repeat
- This header is always included with 401 status code.
It tells the browser what authorization type(BASIC or DIGEST) and realm the
client should supply in its Authorization header.