javax.servlet
Interface ServletResponse

All Known Subinterfaces:
HttpServletResponse

public abstract interface ServletResponse

Interface for sending MIME data from the servlet's service method to the client. Network service developers implement this interface; its methods are then used by servlets when the service method is run, to return data to clients. The ServletResponse object is passed as an argument to the service method.

To write MIME bodies which consist of binary data, use the output stream returned by getOutputStream. To write MIME bodies consisting of text data, use the writer returned by getWriter. If you need to mix binary and text data, for example because you're creating a multipart response, use the output stream to write the multipart headers, and use that to build your own text bodies.

If you don't explicitly set the character set in your MIME media type, with setContentType, one will be selected and the content type will be modified accordingly. If you will be using a writer, and want to call the setContentType method, you must do so before calling the getWriter method. If you will be using the output stream, and want to call setContentType, you must do so before using the output stream to write the MIME body.

For more information about MIME, see the Internet RFCs such as RFC 2045, the first in a series which defines MIME. Note that protocols such SMTP and HTTP define application-specific profiles of MIME, and that standards in this area are evolving.


Method Summary
 java.lang.String getCharacterEncoding()
          Returns the character set encoding used for this MIME body.
 ServletOutputStream getOutputStream()
          Returns an output stream for writing binary response data.
 java.io.PrintWriter getWriter()
          Returns a print writer for writing formatted text responses.
 void setContentLength(int len)
          Sets the content length for this response.
 void setContentType(java.lang.String type)
          Sets the content type for this response.
 

Method Detail

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Returns the character set encoding used for this MIME body. The character encoding is either the one specified in the assigned content type, or one which the client understands. If no content type has yet been assigned, it is implicitly set to text/plain

See RFC 2047 for more infomration about character encoding and MIME.


getOutputStream

public ServletOutputStream getOutputStream()
                                    throws java.io.IOException
Returns an output stream for writing binary response data.
Throws:
IllegalStateException - if getWriter has been called on this same request.
java.io.IOException - if an I/O exception has occurred
See Also:
getWriter

getWriter

public java.io.PrintWriter getWriter()
                              throws java.io.IOException
Returns a print writer for writing formatted text responses. The MIME type of the response will be modified, if necessary, to reflect the character encoding used, through the charset=... property. This means that the content type must be set before calling this method.
Throws:
java.io.UnsupportedEncodingException - if no such encoding can be provided
IllegalStateException - if getOutputStream has been called on this same request.
java.io.IOException - on other errors.
See Also:
getOutputStream, setContentType

setContentLength

public void setContentLength(int len)
Sets the content length for this response.
Parameters:
len - the content length

setContentType

public void setContentType(java.lang.String type)
Sets the content type for this response. This type may later be implicitly modified by addition of properties such as the MIME charset=<value> if the service finds it necessary, and the appropriate media type property has not been set.

This response property may only be assigned one time. If a writer is to be used to write a text response, this method must be called before the method getWriter. If an output stream will be used to write a response, this method must be called before the output stream is used to write response data.

Parameters:
type - the content's MIME type
See Also:
getOutputStream, getWriter