Synopsis
Interface Name: javax.servlet.ServletResponse
Superinterface: None
Immediate Subinterfaces: javax.servlet.http.HttpServletResponse
Interfaces Implemented: None
Availability: Servlet API 1.0 and later
Description
Servlets use ServletResponse objects to send MIME-encoded data back to the client. To send binary data, use the ServletOutputStream returned by getOutputStream( ). In order to send character data, use the PrintWriter returned by getWriter( ). You can explicitly set the output's MIME type using the setContentType( ) method and can set the response locale using the setLocale( ) method. Make these calls before calling getWriter( ), as getWriter( ) consults the content type and locale to determine which charset to use. Consult RFC 2045 at http://www.ietf.org/rfc/rfc2045.txt for more information on MIME.
Interface Declaration
public interface ServletResponse { // Methods public abstract void flushBuffer() throws IOException; // New in 2.2 public abstract int getBufferSize(); // New in 2.2 public abstract String getCharacterEncoding(); // New in 2.0 public abstract Locale getLocale(); // New in 2.2 public abstract ServletOutputStream getOutputStream() throws IOException, IllegalStateException; public abstract PrintWriter getWriter() // New in 2.0 throws IOException, IllegalStateException; public abstract boolean isCommitted(); // New in 2.2 public abstract void reset() throws IllegalStateException; // New in 2.2 public abstract void setBufferSize(int size) // New in 2.2 throws IllegalStateException; public abstract void setContentLength(int len); public abstract void setContentType(String type); public abstract void setLocale(Locale loc); // New in 2.2 }
Methods
flushBuffer()
public abstract void flushBuffer() throws IOException
- Description
-
Forces any content in the buffer to be written to the client. Calling this method automatically commits the response, meaning the status code and headers will be written and a reset( ) will no longer be possible. This method was introduced in Servlet API 2.2.
getBufferSize()
public abstract int getBufferSize()
- Description
-
Returns an int indicating how large the current buffer actually is or 0 in the unlikely event no buffering is used. This method was introduced in Servlet API 2.2.
getCharacterEncoding()
public abstract String getCharacterEncoding()
- Description
-
Returns the charset encoding used for this MIME body. This is the charset specified by the assigned content type or ISO-8859-1 if no charset has been specified. This method was introduced in Servlet API 2.0.
getLocale()
public abstract Locale getLocale()
- Description
-
Returns the Locale currently assigned to the response. This method was introduced in Servlet API 2.2.
getOutputStream()
public abstract ServletOutputStream getOutputStream() throws IOException, IllegalStateException
- Description
-
Returns a ServletOutputStream for writing binary (byte-at-a-time) response data. No encoding is performed. Throws an IllegalStateException if getWriter( ) has already been called on this response.
getWriter()
public abstract PrintWriter getWriter() throws IOException
- Description
-
Returns a PrintWriter for writing character-based response data. The writer encodes the characters according to whatever charset is given in the content type. If no charset is specified in the content type, as is generally the case, the writer uses the ISO-8859-1 (Latin-1) encoding appropriate for Western European languages. Throws an IllegalStateException if getOutputStream( ) has already been called on this response and an UnsupportedEncodingException if the encoding of the output stream is unsupported or unknown. This was introduced in Servlet API 2.0.
isCommitted()
public abstract boolean isCommitted()
- Description
-
Returns a boolean indicating whether any part of the response has actually been sent. If this method returns true, it's too late to change the status code and headers. This method was introduced in Servlet API 2.2.
reset()
public abstract void reset() throws IllegalStateException
- Description
-
Clears the response buffer as well as the currently assigned status code and response headers. This method must be called before the response has been committed, otherwise it throws an IllegalStateException. reset( ) is automatically called by the methods sendError( ) and sendRedirect( ). This method was introduced in Servlet API 2.2.
setBufferSize()
public abstract void setBufferSize(int size) throws IllegalStateException
- Description
-
Tells the server the minimum response buffer size, in bytes, that the servlet will accept. The server may provide a larger buffer than requested to keep buffers in 8K blocks, for example, to facilitate reuse. 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. This method was introduced in Servlet API 2.2.
setContentLength()
public abstract void setContentLength(int len)
- Description
-
Sets the length of the content being returned by the server. In HTTP servlets, it sets the HTTP Content-Length header. HTTP servlets use this method to enable persistent connections and help client progress monitors. Its use is optional. If the response content fits entirely within the assigned response buffer size, the server can automatically set the content length.
setContentType()
public abstract void setContentType(String type)
- Description
-
This method sets the content type of the response to be the specified type. In HTTP servlets, it sets the Content-Type HTTP header.
setLocale()
public abstract void setLocale(Locale loc)
- Description
-
Sets the locale of the response. The server modifies the Content-Language and Content-Type headers as appropriate for the given locale. This method should be called after setting the Content-Type and before calling getWriter( ). By default, the response locale is the default locale for the server. This method was introduced in Servlet API 2.2.