javax.servlet
Interface ServletResponse

All Known Subinterfaces:
CauchoResponse, HttpServletResponse
All Known Implementing Classes:
AbstractCauchoResponse, CauchoResponseWrapper, CauchoResponseWrapper, DispatchResponse, HttpServletResponseImpl, HttpServletResponseWrapper, IncludeResponse, JspResponse, JspResponseWrapper, ProxyCacheResponse, ResponseAdapter, ResponseWrapper, ServletResponseWrapper, StubServletResponse, ToCharResponseAdapter

public interface ServletResponse

ServletReponse control the output to the client.

A typical servlet will output its response as follows:

 response.setContentType("text/html; charset=UTF-8");
 PrintWriter pw = response.getWriter();
 pw.println("Hello, world");
 

Buffering

The servlet engine buffers output before sending it to the client. Buffering adds efficiency and flexibility. Until data is actually committed to the client, the servlet can change headers or reset the entire results.

Character encoding

Use either setContentType or setLocale to set the character encoding.
 response.setContentType("text/html; charset=ISO-8859-2");
 


Method Summary
 void flushBuffer()
          Flushes the buffer to the client.
 int getBufferSize()
          Returns the size of the output buffer.
 java.lang.String getCharacterEncoding()
          Returns the character encoding the response is using for output.
 java.lang.String getContentType()
          Returns the content type for the response.
 java.util.Locale getLocale()
          Returns the output locale.
 ServletOutputStream getOutputStream()
          Returns an output stream for writing to the client.
 java.io.PrintWriter getWriter()
          Returns a PrintWriter with the proper character encoding for writing text data to the client.
 boolean isCommitted()
          Returns true if some data has actually been send to the client.
 void reset()
          Resets the output stream, clearing headers and the output buffer.
 void resetBuffer()
          Resets the output stream, clearing headers and the output buffer.
 void setBufferSize(int size)
          Sets the output buffer size to size.
 void setCharacterEncoding(java.lang.String charset)
          Sets the character encoding the response is using for output.
 void setContentLength(int len)
          Explicitly sets the length of the result value.
 void setContentType(java.lang.String type)
          Sets the response content type.
 void setLocale(java.util.Locale locale)
          Sets the output locale.
 

Method Detail

setContentType

void setContentType(java.lang.String type)
Sets the response content type. The content type includes the character encoding. The content type must be set before calling getWriter() so the writer can use the proper character encoding.

To set the output character encoding to ISO-8859-2, use the following:

 response.setContentType("text/html; charset=ISO-8859-2");
 

Parameters:
type - the mime type of the output

getContentType

java.lang.String getContentType()
Returns the content type for the response.

Since:
2.4

getCharacterEncoding

java.lang.String getCharacterEncoding()
Returns the character encoding the response is using for output. If no character encoding is specified, ISO-8859-1 will be used.


setCharacterEncoding

void setCharacterEncoding(java.lang.String charset)
Sets the character encoding the response is using for output. If no character encoding is specified, ISO-8859-1 will be used.

Since:
2.4

setLocale

void setLocale(java.util.Locale locale)
Sets the output locale. The response will set the character encoding based on the locale. For example, setting the "kr" locale will set the character encoding to "EUC_KR".


getLocale

java.util.Locale getLocale()
Returns the output locale.


getOutputStream

ServletOutputStream getOutputStream()
                                    throws java.io.IOException
Returns an output stream for writing to the client. You can use the output stream to write binary data.

Throws:
java.io.IOException

getWriter

java.io.PrintWriter getWriter()
                              throws java.io.IOException
Returns a PrintWriter with the proper character encoding for writing text data to the client.

Throws:
java.io.IOException

setBufferSize

void setBufferSize(int size)
Sets the output buffer size to size. The servlet engine may round the size up.

Parameters:
size - the new output buffer size.

getBufferSize

int getBufferSize()
Returns the size of the output buffer.


flushBuffer

void flushBuffer()
                 throws java.io.IOException
Flushes the buffer to the client.

Throws:
java.io.IOException

isCommitted

boolean isCommitted()
Returns true if some data has actually been send to the client. The data will be sent if the buffer overflows or if it's explicitly flushed.


reset

void reset()
Resets the output stream, clearing headers and the output buffer. Calling reset() after data has been committed is illegal.

Throws:
java.lang.IllegalStateException - if isCommitted() is true.

resetBuffer

void resetBuffer()
Resets the output stream, clearing headers and the output buffer. Calling reset() after data has been committed is illegal.

Throws:
java.lang.IllegalStateException - if isCommitted() is true.

setContentLength

void setContentLength(int len)
Explicitly sets the length of the result value. Normally, the servlet engine will handle this.