Using the HttpServletResponse interface, write code to
set an HTTP response header,
set the content type of the response, acquire a text stream for the response, acquire a
binary stream for the response, redirect an HTTP request to another URL, or add cookies to
the response.
Headers.
A servlet can set headers of an HTTP response via the following methods of the
HttpServletResponse interface:
setHeader
Sets a response header with the given name and value. If the header had
already been set, the new value OVERWRITES the previous one. The
containsHeader method can be used to test for the
presence of a header before setting its value.
addHeader
Adds a response header with the given name and value. This method allows
response headers to have multiple values.
public interface HttpServletResponse extends javax.servlet.ServletResponse {
public void setHeader(java.lang.String name, java.lang.String value);
public void addHeader(java.lang.String name, java.lang.String value);
}
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.
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.
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:
setIntHeader
Sets a response header with the given name and integer value. If the header
had already been set, the new value overwrites the previous one. The
containsHeader method can be used to test for the
presence of a header before setting its value.
setDateHeader
Sets a response header with the given name and date-value. The date is
specified in terms of milliseconds since the epoch. If the header had
already been set, the new value overwrites the previous one. The
containsHeader method can be used to test for
the presence of a header before setting its value.
addIntHeader
Adds a response header with the given name and integer value. This method
allows response headers to have multiple values.
addDateHeader
Adds a response header with the given name and date-value. The date is specified
in terms of milliseconds since the epoch. This method allows response
headers to have multiple values.
public interface HttpServletResponse extends javax.servlet.ServletResponse {
public void setIntHeader(java.lang.String name, int value);
public void setDateHeader(java.lang.String name, long date);
public void addIntHeader(java.lang.String name, int value);
public void addDateHeader(java.lang.String name, long date);
}
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.
Content type.
The charset for the MIME body response can be specified explicitly using the
setContentType(String) method. Explicit specifications take
precedence over implicit specifications. If no charset is specified, ISO-8859-1
will be used. The setContentType method MUST be called BEFORE
getWriter and BEFORE committing the response for the character
encoding to be used.
There are 2 ways to define content type:
Acquire a text stream.
To send CHARACTER data, use the PrintWriter object returned by
ServletResponse.getWriter()
public interface ServletResponse {
public java.io.PrintWriter getWriter() throws IOException;
}
Returns a PrintWriter object that can send character text to the
client. The PrintWriter uses the character encoding returned by
getCharacterEncoding(). Calling flush() on the
PrintWriter commits the response.
Either this method or getOutputStream() may be called to write the body,
NOT BOTH.
Acquire a binary stream.
ServletResponse.getOutputStream() provides an output stream for
sending BINARY data to the client. A ServletOutputStream object is
normally retrieved via this method.
public interface ServletResponse {
public ServletOutputStream getOutputStream() throws IOException;
}
The servlet container does NOT encode the binary data.
Calling flush() on the ServletOutputStream commits
the response. Either this method or getWriter() may be called to
write the body, NOT BOTH.
Redirect an HTTP request to another URL.
The HttpServletResponse.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.
public interface HttpServletResponse extends javax.servlet.ServletResponse {
public void sendRedirect(java.lang.String location) throws IOException;
}
Sends a temporary redirect response to the client using the specified redirect
location URL. This method can accept relative URLs; the servlet container
must convert the relative URL to an absolute URL before sending the
response to the client. If the location is relative without a leading ’/’ the container
interprets it as relative to the current request URI. If the location is relative
with a leading ’/’ the container interprets it as relative to the servlet
container root.
If the response has already been committed, this method throws an
IllegalStateException. After using this method, the response
should be considered to be committed and should not be written to.
This method 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 this method are called, the data is
ignored.
If data has been written to the response buffer, but not returned to the client
(i.e. the response is not committed), the data in the response buffer must be
cleared and replaced with the data set by these methods. If the response is
committed, this method must throw an IllegalStateException.
Add cookies to the response.
The servlet sends cookies to the browser by using the
HttpServletResponse.addCookie(Cookie) method, which adds fields to
HTTP response headers to send cookies to the browser, one at a time. The
browser is expected to support 20 cookies for each Web server, 300 cookies total,
and may limit cookie size to 4 KB each.
public interface HttpServletResponse extends javax.servlet.ServletResponse {
public void addCookie(Cookie cookie);
}
Adds the specified cookie to the response. This method can be called multiple
times to set more than one cookie.
Visit
http://java.boot.by
for the updates.