|[ Team LiB ]|
The ServletResponse Class
When a servlet needs to send data back to the browser, it uses the ServletResponse object. Using this object, you can get an output stream for sending data back, set the content type and length, and set the character encoding if you need to send back character data.
Setting the Content Type and Length
You can set the content type and length by calling setContentType and setContentLength:
public void setContentType(String contentType) public void setContentLength(int contentLength)
When you set the content type, you can also set the character encoding. For example, you could set the content type to text/html; charset=ISO-8859-4.
Opening an Output Stream
There are two methods for obtaining an output stream:
public ServletOutputStream getOutputStream() throws IOException public PrintWriter getWriter() throws IOException
When you need to send character data back to the browser, you should use the getWriter method. If you need to send binary data, use getOutputStream.
Setting Locale-Specific Information
When you send a response back to the browser, you might want to target it to a specific locale, which implies a specific language and possibly country. For example, if you need to write out a date or a currency value, you want to write it in a format that the end user understands. The ServletRequest object tells you which locales the browser supports. When you send a request, you can set a locale in the output stream. The locale can imply a particular character encoding and can even change the character encoding based on the content type. To set the locale for the response, call setLocale:
public void setLocale(Locale aLocale)
You can get the locale and character encoding for the response by calling getLocale and getCharacterEncoding:
public Locale getLocale() public String getCharacterEncoding()
Internet Clients—Parlez-vous français?
We'll discuss internationalization in additional detail in Hour 22.
Most of the time, a servlet or JSP response is buffered. You can control the amount of memory used for buffering and also clear out the buffer if you need to. Use getBufferSize and setBufferSize to control the amount of buffering:
public int getBufferSize() public void setBufferSize(int bufferSize)
If you want to change the buffer size, you should do it before you try to send any data back to the browser. After you send data back, or at least write it to the buffer, you can't change the buffer size. If you want to send the current buffer contents back to the browser immediately, call flushBuffer:
public void flushBuffer() throws IOException
If you want to clear the contents of the buffer, call reset:
public void reset()
For those occasions where you need to reset the buffer contents without affecting the headers or status code, you use resetBuffer:
public void resetBuffer()
After the response status and headers have been sent back to the browser, the response is said to be "committed." After it is committed, you can't change any header values, which you can normally set in the HttpServletResponse object. You can find out whether the response has been committed by calling isCommitted:
public boolean isCommitted()
|[ Team LiB ]|