|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Defines an object to assist a servlet in sending a response to the client.
The servlet container creates a ServletResponse
object and
passes it as an argument to the servlet's service
method.
To send binary data in a MIME body response, use
the ServletOutputStream
returned by getOutputStream()
.
To send character data, use the PrintWriter
object
returned by getWriter()
. To mix binary and text data,
for example, to create a multipart response, use a
ServletOutputStream
and manage the character sections
manually.
The charset for the MIME body response can be specified with
setContentType(java.lang.String)
. For example, "text/html; charset=Shift_JIS".
The charset can alternately be set using setLocale(java.util.Locale)
.
If no charset is specified, ISO-8859-1 will be used.
The setContentType
or setLocale
method
must be called before getWriter
for the charset to
affect the construction of the writer.
See the Internet RFCs such as RFC 2045 for more information on MIME. Protocols such as SMTP and HTTP define profiles of MIME, and those standards are still evolving.
ServletOutputStream
Method Summary | |
void |
flushBuffer()
Forces any content in the buffer to be written to the client. |
int |
getBufferSize()
Returns the actual buffer size used for the response. |
java.lang.String |
getCharacterEncoding()
Returns the name of the charset used for the MIME body sent in this response. |
java.util.Locale |
getLocale()
Returns the locale assigned to the response. |
ServletOutputStream |
getOutputStream()
Returns a ServletOutputStream suitable for writing binary
data in the response. |
java.io.PrintWriter |
getWriter()
Returns a PrintWriter object that
can send character text to the client. |
boolean |
isCommitted()
Returns a boolean indicating if the response has been committed. |
void |
reset()
Clears any data that exists in the buffer as well as the status code and headers. |
void |
resetBuffer()
Clears the content of the underlying buffer in the response without clearing headers or status code. |
void |
setBufferSize(int size)
Sets the preferred buffer size for the body of the response. |
void |
setContentLength(int len)
Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header. |
void |
setContentType(java.lang.String type)
Sets the content type of the response being sent to the client. |
void |
setLocale(java.util.Locale loc)
Sets the locale of the response, setting the headers (including the Content-Type's charset) as appropriate. |
Method Detail |
public java.lang.String getCharacterEncoding()
If no charset has been assigned, it is implicitly
set to ISO-8859-1
(Latin-1
).
See RFC 2047 (http://ds.internic.net/rfc/rfc2045.txt) for more information about character encoding and MIME.
String
specifying the
name of the charset, for
example, ISO-8859-1
public ServletOutputStream getOutputStream() throws java.io.IOException
ServletOutputStream
suitable for writing binary
data in the response. 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.
ServletOutputStream
for writing binary data
java.lang.IllegalStateException
- if the getWriter
method
has been called on this response
java.io.IOException
- if an input or output exception occurredgetWriter()
public java.io.PrintWriter getWriter() throws java.io.IOException
PrintWriter
object that
can send character text to the client.
The character encoding used is the one specified
in the charset=
property of the
setContentType(java.lang.String)
method, which must be called
before calling this method for the charset to take effect.
If necessary, the MIME type of the response is modified to reflect the character encoding used.
Calling flush() on the PrintWriter commits the response.
Either this method or getOutputStream()
may be called
to write the body, not both.
PrintWriter
object that
can return character data to the client
java.io.UnsupportedEncodingException
- if the charset specified in
setContentType
cannot be
used
java.lang.IllegalStateException
- if the getOutputStream
method has already been called for this
response object
java.io.IOException
- if an input or output exception occurredgetOutputStream()
,
setContentType(java.lang.String)
public void setContentLength(int len)
len
- an integer specifying the length of the
content being returned to the client; sets
the Content-Length headerpublic void setContentType(java.lang.String type)
text/html; charset=ISO-8859-4
.
If obtaining a PrintWriter
, this method should be
called first.
type
- a String
specifying the MIME
type of the contentgetOutputStream()
,
getWriter()
public void setBufferSize(int size)
getBufferSize
.
A larger buffer allows more content to be written before anything is actually sent, thus providing the servlet with more time to set appropriate status codes and headers. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.
This method must be called before any response body content is
written; if content has been written, this method throws an
IllegalStateException
.
size
- the preferred buffer size
java.lang.IllegalStateException
- if this method is called after
content has been writtengetBufferSize()
,
flushBuffer()
,
isCommitted()
,
reset()
public int getBufferSize()
setBufferSize(int)
,
flushBuffer()
,
isCommitted()
,
reset()
public void flushBuffer() throws java.io.IOException
java.io.IOException
setBufferSize(int)
,
getBufferSize()
,
isCommitted()
,
reset()
public void resetBuffer()
IllegalStateException
.
setBufferSize(int)
,
getBufferSize()
,
isCommitted()
,
reset()
public boolean isCommitted()
setBufferSize(int)
,
getBufferSize()
,
flushBuffer()
,
reset()
public void reset()
IllegalStateException
.
java.lang.IllegalStateException
- if the response has already been
committedsetBufferSize(int)
,
getBufferSize()
,
flushBuffer()
,
isCommitted()
public void setLocale(java.util.Locale loc)
getWriter()
. By default, the response locale
is the default locale for the server.
loc
- the locale of the responsegetLocale()
public java.util.Locale getLocale()
setLocale(java.util.Locale)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |