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.
setDateHeader(String header, long milliseconds)
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.
setIntHeader(String header, int headerValue)
This method spares you the minor inconvenience of converting an int to a String before inserting it into a header.
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.
This method inserts a cookie into the Set-Cookie header. There is no corresponding setCookie method, since it is normal to have multiple Set-Cookie lines.
the sendRedirect method sets the Location header as well as setting the status
code to 302.
From novice to tech pro — start learning today.