javax.servlet
Interface ServletResponse

All Known Subinterfaces:
HttpServletResponse

public abstract interface ServletResponse

Sends MIME-encoded data from the servlet to the client. The servlet engine 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(). Likewise, to send text data, use the PrintWriter object returned by getWriter(). If you need to mix binary and text data, for example, if you are creating a multipart response, use a ServletOutputStream to write the multipart headers, and then use the headers to build your own text bodies.

If you do not specify a character set for the MIME body response with setContentType(java.lang.String), the servlet engine will select one and modify the content accordingly. Call setContentType before you call getWriter or getOutputStream.

See the Internet RFCs such as RFC 2045 at RFC 2045 for more information on MIME. Protocols such as SMTP and HTTP define profiles of MIME, and those standards are still evolving.

Version:
$Version$
Author:
Various
See Also:
ServletOutputStream

Method Summary
 java.lang.String getCharacterEncoding()
          Returns the name of the character set encoding used for the MIME body sent by this response.
 ServletOutputStream getOutputStream()
          Returns a ServletOutputStream suitable for writing binary data in the response.
 java.io.PrintWriter getWriter()
          Returns a PrintWriter object that you can use to send character text to the client.
 void setContentLength(int len)
          Sets the length of the content the server returns to the client.
 void setContentType(java.lang.String type)
          Sets the content type of the response the server sends to the client.
 

Method Detail

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Returns the name of the character set encoding used for the MIME body sent by this response.

The character encoding is either the one specified in the content itself or another one the client understands. If no character encoding has been assigned, it is implicitly set to text/plain.

See RFC 2047 (http://ds.internic.net/rfc/rfc2045.txt) for more information about character encoding and MIME.

Returns:
a String specifying the name of the character set encoding, for example, text/plain

getOutputStream

public ServletOutputStream getOutputStream()
                                    throws java.io.IOException
Returns a ServletOutputStream suitable for writing binary data in the response. The servlet engine does not encode the binary data.
Returns:
a ServletOutputStream for writing binary data
Throws:
IllegalStateException - if you have already called the getWriter method for the same response
java.io.IOException - if an input or output exception occurred
See Also:
getWriter()

getWriter

public java.io.PrintWriter getWriter()
                              throws java.io.IOException
Returns a PrintWriter object that you can use to 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 you must call before you call this method.

If necessary, the MIME type of the response is modified to reflect the character encoding used.

You cannot use this method if you have already called getOutputStream() for this ServletResponse object.

Returns:
a PrintWriter object that can return text to the client
Throws:
java.io.UnsupportedEncodingException - if the character encoding specified in setContentType cannot be used
IllegalStateException - if the getOutputStream method has already been called for this response object; in that case, you can't use this method
java.io.IOException - if an input or output exception occurred
See Also:
getOutputStream(), setContentType(java.lang.String)

setContentLength

public void setContentLength(int len)
Sets the length of the content the server returns to the client. In HTTP servlets, this method sets the HTTP Content-Length header.
Parameters:
len - an integer specifying the length of the content being returned to the client; sets the Content-Length header

setContentType

public void setContentType(java.lang.String type)
Sets the content type of the response the server sends to the client. The content type may include the type of character encoding used, for example, text/html; charset=ISO-8859-4.

You can only use this method once, and you should call it before you obtain a PrintWriter or ServletOutputStream object to return a response.

Parameters:
type - a String specifying the MIME type of the content
See Also:
getOutputStream(), getWriter()