javax.servlet
Interface ServletResponse

All Known Subinterfaces:
HttpServletResponse
All Known Implementing Classes:
FilteringResponseWrapper, javax.servlet.http.HttpServletResponseWrapper, javax.servlet.ServletResponseWrapper

public interface ServletResponse

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". 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.

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

Method Summary
 String getCharacterEncoding()
          Returns the name of the charset used for the MIME body sent in this response.
 ServletOutputStream getOutputStream()
          Returns a ServletOutputStream suitable for writing binary data in the response.
 PrintWriter getWriter()
          Returns a PrintWriter object that can send character text to the client.
 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(String type)
          Sets the content type of the response being sent to the client.
 

Method Detail

getCharacterEncoding

String getCharacterEncoding()
Returns the name of the charset used for the MIME body sent in this response.

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.

Returns:
a String specifying the name of the charset, for example, ISO-8859-1

getOutputStream

ServletOutputStream getOutputStream()
                                    throws IOException
Returns a 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.

Returns:
a ServletOutputStream for writing binary data
Throws:
IllegalStateException - if the getWriter method has been called on this response
IOException - if an input or output exception occurred
See Also:
getWriter()

getWriter

PrintWriter getWriter()
                      throws IOException
Returns a 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.

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

setContentLength

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.

Parameters:
len - an integer specifying the length of the content being returned to the client; sets the Content-Length header

setContentType

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

If obtaining a PrintWriter, this method should be called first.

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


Copyright © 2011 Apache Software Foundation. All Rights Reserved.