Clarifications for Using Methods
Chapter 7 API Clarifications 75
For JSPs, set the response content type using a page directive, for example:
<%@ page contentType="text/html; charset=gb2312"%>
ServletResponse.getOutputStream and
getWriter
public ServletOutputStream getOutputStream() throws
java.io.IOException
Returns a ServletOutputStream suitable for writing binary data in the response.
The servlet container does not encode the binary data. Either this method or
getWriter may be called to write the body, not both.
public java.io.PrintWriter getWriter() throws java.io.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.
Either this method or
getOutputStream may be called to write the body, not both.
Clarification
The specification recommends that when a servlet calls the getWriter method on a
response for which the
getOutputStream method has already been called, or the
other way around, the servlet container should throw an
IllegalStateException.
iPlanet Web Server 6.0 doesn’t throw the exception, while ensuring that the writes
to writer and the output stream are ordered. This implementation is more lenient
but preserves correctness and permits scenarios like these:
• A servlet calls
getOutputStream and throws an exception, and a JSP is used
for the exception page. With strict compliance, this fails, because the JSP tries to
call
getWriter on the response object.
• A JSP that calls
getWriter includes a servlet that calls getOutputStream.
Komentarze do niniejszej Instrukcji