ServletResponse (Java EE 5)

Java EE API


javax.servlet Interface ServletResponse

All Known Subinterfaces:
HttpServletResponse
All Known Implementing Classes:
HttpServletResponseWrapper, ServletResponseWrapper

public interface ServletResponse


定义辅助 servlet 将响应发送到客户端的对象。servlet 容器创建 ServletResponse 对象,并将它作为参数传递给 servlet 的 service 方法。

要发送 MIME 正文响应中的二进制数据,请使用 #getOutputStream 返回的 ServletOutputStream。要发送字符数据,请使用 #getWriter 返回的 PrintWriter 对象。要混合二进制数据和文本数据,例如要创建 multipart 响应,请使用 ServletOutputStream 并手动管理字符部分。

可使用 #setCharacterEncoding#setContentType 显式指定 MIME 正文响应的 charset,或使用 #setLocale 方法隐式指定它。显式指定优先于隐式指定。如果未指定 charset,则将使用 ISO-8859-1。setCharacterEncodingsetContentTypesetLocale 方法必须在调用 getWriter 之前,并且必须在提交采用要使用的字符编码的响应之前调用。

有关 MIME 的更多信息,请参见 Internet RFC,比如 RFC 2045。诸如 SMTP 和 HTTP 之类的协议定义了 MIME 的配置文件,并且那些标准仍在不断发展。

英文文档:

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 explicitly using the setCharacterEncoding(java.lang.String) and setContentType(java.lang.String) methods, or implicitly using the setLocale(java.util.Locale) method. Explicit specifications take precedence over implicit specifications. If no charset is specified, ISO-8859-1 will be used. The setCharacterEncoding, setContentType, or setLocale method must be called before getWriter and before committing the response for the character encoding to be used.

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.

Author:
Various
See Also:
ServletOutputStream

Method Summary
 void
 int
 String
 String
 Locale
 ServletOutputStream
 PrintWriter
 boolean
 void
 void
 void
 void
 void
 void
 void
 

Method Detail

public String getCharacterEncoding()
返回用于此响应中发送的正文的字符编码 (MIME charset) 名称。可能已使用 #setCharacterEncoding#setContentType 方法显式指定字符编码,或使用 #setLocale 方法隐式指定它。显式指定优先于隐式指定。在已调用 getWriter 之后或者在已提交响应之后调用这些方法不会对字符编码产生任何影响。如果未指定字符编码,则返回 ISO-8859-1

有关字符编码和 MIME 的更多信息,请参见 RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)。

return 指定字符编码名称的 String,例如 UTF-8

英文文档:

getCharacterEncoding

String getCharacterEncoding()
Returns the name of the character encoding (MIME charset) used for the body sent in this response. The character encoding may have been specified explicitly using the setCharacterEncoding(java.lang.String) or setContentType(java.lang.String) methods, or implicitly using the setLocale(java.util.Locale) method. Explicit specifications take precedence over implicit specifications. Calls made to these methods after getWriter has been called or after the response has been committed have no effect on the character encoding. If no character encoding has been specified, ISO-8859-1 is returned.

See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) for more information about character encoding and MIME.

Returns:
a String specifying the name of the character encoding, for example, UTF-8

public String getContentType()
返回用于此响应中发送的 MIME 正文的内容类型。必须在提交响应之前已使用 #setContentType 指定适当的内容类型。如果未指定内容类型,则此方法返回 null。如果已指定内容类型,并且已经如 #getCharacterEncoding 中所述显式或隐式指定了字符编码或者已调用 #getWriter,则返回的字符串中将包含 charset 参数。如果未指定字符编码,则省略 charset 参数。
return 指定内容类型的 String,例如 text/html; charset=UTF-8,或者返回 null
since2.4
英文文档:

getContentType

String getContentType()
Returns the content type used for the MIME body sent in this response. The content type proper must have been specified using setContentType(java.lang.String) before the response is committed. If no content type has been specified, this method returns null. If a content type has been specified, and a character encoding has been explicitly or implicitly specified as described in getCharacterEncoding() or getWriter() has been called, the charset parameter is included in the string returned. If no character encoding has been specified, the charset parameter is omitted.

Returns:
a String specifying the content type, for example, text/html; charset=UTF-8, or null
Since:
2.4

public ServletOutputStream getOutputStream() throws java.io.IOException
返回适用于在响应中编写二进制数据的 ServletOutputStream。servlet 容器不会编码二进制数据。

对 ServletOutputStream 调用 flush() 将提交响应。 可调用此方法或 #getWriter 编写正文,而不是两种方法都调用。

return 用于编写二进制数据的 ServletOutputStream
ThrowsIllegalStateException: 如果已对此响应调用 getWriter 方法
Throwsjava.io.IOException: 如果发生输入或输出异常
See also getWriter

英文文档:

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()

public java.io.PrintWriter getWriter() throws java.io.IOException
返回可将字符文本发送到客户端的 PrintWriter 对象。PrintWriter 使用 #getCharacterEncoding 返回的字符编码。如果未如 getCharacterEncoding 中所述指定响应的字符编码(即该方法只返回默认值 ISO-8859-1),则 getWriter 会将字符编码更新到 ISO-8859-1

PrintWriter 调用 flush() 将提交响应。

可调用此方法或 #getOutputStream 编写正文,而不是两种方法都调用。

return 可将字符数据返回到客户端的 PrintWriter 对象。
ThrowsUnsupportedEncodingException: 如果无法使用 getCharacterEncoding 返回的字符编码
ThrowsIllegalStateException: 如果已对此响应对象调用 getOutputStream 方法
Throwsjava.io.IOException: 如果发生输入或输出异常
See also getOutputStream, setCharacterEncoding

英文文档:

getWriter

PrintWriter getWriter()
                      throws IOException
Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.

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 character encoding returned by getCharacterEncoding 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(), setCharacterEncoding(java.lang.String)

public void setCharacterEncoding(String charset)
设置将发送到客户端的响应的字符编码 (MIME charset),例如,将它设置为 UTF-8。如果已通过 #setContentType#setLocale 设置了字符编码,则此方法将重写该字符编码。用 text/htmlString 调用 #setContentType 并用 UTF-8String 调用此方法等效于用 text/html; charset=UTF-8String 调用 setContentType

可重复调用此方法来更改字符编码。如果在已调用 getWriter 之后或者在已提交响应之后调用此方法,则此方法没有任何效果。

容器必须让客户端了解将用于 servlet 响应的 writer 的字符编码,如果协议提供了实现上述操作的方法。在使用 HTTP 的情况下,字符编码是作为文本媒体类型的 Content-Type 头的一部分传送的。注意,如果 servlet 未指定内容类型,则字符编码不能通过 HTTP 头传送;但是,它仍然用于编码通过 servlet 响应的 writer 编写的文本。

charset 仅指定 IANA Character Set (http://www.iana.org/assignments/character-sets) 定义的字符集的 String
since2.4
See also #setLocale

英文文档:

setCharacterEncoding

void setCharacterEncoding(String charset)
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by setContentType(java.lang.String) or setLocale(java.util.Locale), this method overrides it. Calling setContentType(java.lang.String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8.

This method can be called repeatedly to change the character encoding. This method has no effect if it is called after getWriter has been called or after the response has been committed.

Containers must communicate the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the Content-Type header for text media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the servlet response's writer.

Parameters:
charset - a String specifying only the character set defined by IANA Character Sets (http://www.iana.org/assignments/character-sets)
Since:
2.4
See Also:
#setLocale

public void setContentLength(int len)
设置 HTTP servlet 中响应的内容正文的长度,此方法设置 HTTP Content-Length 头。
len 指定将返回到客户端的内容长度的整数;设置 Content-Length 头
英文文档:

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

public void setContentType(String type)
设置将发送到客户端的响应的内容类型,如果该响应尚未提交。给定内容类型可能包含字符编码规范,例如 text/html;charset=UTF-8。如果在调用 getWriter 之前调用此方法,则只根据给定内容类型设置响应的字符编码。

可重复调用此方法来更改内容类型和字符编码。如果在已提交响应之后调用此方法,则此方法没有任何效果。如果在已调用 getWriter 之后或者在已提交响应之后调用此方法,则该方法不会设置响应的字符编码。

容器必须让客户端了解用于 servlet 响应的 writer 的内容类型和字符编码,如果协议提供了实现上述操作的方法。在使用 HTTP 的情况下,使用 Content-Type 头。

type 指定内容的 MIME 类型的 String
See also setLocale, setCharacterEncoding, getOutputStream, getWriter

英文文档:

setContentType

void setContentType(String type)
Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. The response's character encoding is only set from the given content type if this method is called before getWriter is called.

This method may be called repeatedly to change content type and character encoding. This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.

Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.

Parameters:
type - a String specifying the MIME type of the content
See Also:
setLocale(java.util.Locale), setCharacterEncoding(java.lang.String), getOutputStream(), getWriter()

public void setBufferSize(int size)
设置响应正文的首选缓冲区大小。servlet 容器将使用至少与所请求大小一样大的缓冲区。可使用 getBufferSize 获得使用的实际缓冲区大小。

较大的缓冲区允许在实际发送任何东西之前编写更多的内容,这为 servlet 提供了更多时间来设置适当的状态代码和头。较小的缓冲区会减少服务器内存负载,并允许客户端更快地开始接收数据。

必须在编写任何响应正文内容之前调用此方法;如果已经编写了内容或者已经提交了响应对象,则此方法将抛出 IllegalStateException

size 首选缓冲区大小
ThrowsIllegalStateException: 如果在已编写内容后调用此方法
See also getBufferSize, flushBuffer, isCommitted, reset

英文文档:

setBufferSize

void setBufferSize(int size)
Sets the preferred buffer size for the body of the response. The servlet container will use a buffer at least as large as the size requested. The actual buffer size used can be found using getBufferSize.

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 or the response object has been committed, this method throws an IllegalStateException.

Parameters:
size - the preferred buffer size
Throws:
IllegalStateException - if this method is called after content has been written
See Also:
getBufferSize(), flushBuffer(), isCommitted(), reset()

public int getBufferSize()
返回用于该响应的实际缓冲区大小。如果未使用任何缓冲,则此方法返回 0。
return 使用的实际缓冲区大小
See also setBufferSize, flushBuffer, isCommitted, reset
英文文档:

getBufferSize

int getBufferSize()
Returns the actual buffer size used for the response. If no buffering is used, this method returns 0.

Returns:
the actual buffer size used
See Also:
setBufferSize(int), flushBuffer(), isCommitted(), reset()

public void flushBuffer() throws java.io.IOException
强行将缓冲区中的所有内容写入客户端。调用此方法会自动提交响应,这意味着将编写状态代码和头。
See also setBufferSize, getBufferSize, isCommitted, reset
英文文档:

flushBuffer

void flushBuffer()
                 throws IOException
Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.

Throws:
IOException
See Also:
setBufferSize(int), getBufferSize(), isCommitted(), reset()

public void resetBuffer()
清除响应中底层缓冲区的内容,而不清除头或状态代码。如果已提交响应,则此方法将抛出 IllegalStateException
since2.3
See also setBufferSize, getBufferSize, isCommitted, reset
英文文档:

resetBuffer

void resetBuffer()
Clears the content of the underlying buffer in the response without clearing headers or status code. If the response has been committed, this method throws an IllegalStateException.

Since:
2.3
See Also:
setBufferSize(int), getBufferSize(), isCommitted(), reset()

public boolean isCommitted()
返回指示响应是否已提交的 boolean 值。已提交的响应已将其状态代码和头编写好。
return 指示响应是否已提交的 boolean 值
See also setBufferSize, getBufferSize, flushBuffer, reset
英文文档:

isCommitted

boolean isCommitted()
Returns a boolean indicating if the response has been committed. A committed response has already had its status code and headers written.

Returns:
a boolean indicating if the response has been committed
See Also:
setBufferSize(int), getBufferSize(), flushBuffer(), reset()

public void reset()
清除缓冲区中存在的所有数据以及状态代码和头。如果已提交响应,则此方法将抛出 IllegalStateException
ThrowsIllegalStateException: 如果已提交响应
See also setBufferSize, getBufferSize, flushBuffer, isCommitted
英文文档:

reset

void reset()
Clears any data that exists in the buffer as well as the status code and headers. If the response has been committed, this method throws an IllegalStateException.

Throws:
IllegalStateException - if the response has already been committed
See Also:
setBufferSize(int), getBufferSize(), flushBuffer(), isCommitted()

public void setLocale(java.util.Locale loc)
设置响应的语言环境,如果该响应尚未提交。如果尚未使用 #setContentType#setCharacterEncoding 显式设置字符编码、尚未调用 getWriter,并且响应尚未提交,则此方法还将设置适合该语言环境的响应的字符编码。如果部署描述符包含 locale-encoding-mapping-list 元素,并且该元素提供了给定语言环境的映射,则使用该映射。否则,从语言环境到字符编码的映射是与容器相关的。

可重复调用此方法来更改语言环境和字符编码。如果在已提交响应之后调用该方法,则此方法没有任何效果。如果在已使用 charset 规范调用 #setContentType 之后、在已调用 #setCharacterEncoding 之后、在已调用 getWriter 之后,或者在已提交响应之后调用此方法,则此方法不会设置响应的字符编码。

容器必须让客户端了解用于 servlet 响应的 writer 的语言环境和字符编码,如果协议提供了实现上述操作的方法。在使用 HTTP 的情况下,语言环境是通过 Content-Language 头传送的,字符编码是作为文本媒体类型的 Content-Type 头的一部分传送的。注意,如果 servlet 未指定内容类型,则字符编码不能通过 HTTP 头传送;但是,它仍然用于编码通过 servlet 响应的 writer 编写的文本。

loc 响应的语言环境
See also getLocale, setContentType, setCharacterEncoding

英文文档:

setLocale

void setLocale(Locale loc)
Sets the locale of the response, if the response has not been committed yet. It also sets the response's character encoding appropriately for the locale, if the character encoding has not been explicitly set using setContentType(java.lang.String) or setCharacterEncoding(java.lang.String), getWriter hasn't been called yet, and the response hasn't been committed yet. If the deployment descriptor contains a locale-encoding-mapping-list element, and that element provides a mapping for the given locale, that mapping is used. Otherwise, the mapping from locale to character encoding is container dependent.

This method may be called repeatedly to change locale and character encoding. The method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after setContentType(java.lang.String) has been called with a charset specification, after setCharacterEncoding(java.lang.String) has been called, after getWriter has been called, or after the response has been committed.

Containers must communicate the locale and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the locale is communicated via the Content-Language header, the character encoding as part of the Content-Type header for text media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the servlet response's writer.

Parameters:
loc - the locale of the response
See Also:
getLocale(), setContentType(java.lang.String), setCharacterEncoding(java.lang.String)

public java.util.Locale getLocale()
返回使用 #setLocale 方法指定的此响应的语言环境。在已提交响应后调用 setLocale 没有任何效果。如果未指定任何语言环境,则返回容器的默认语言环境。
See also setLocale
英文文档:

getLocale

Locale getLocale()
Returns the locale specified for this response using the setLocale(java.util.Locale) method. Calls made to setLocale after the response is committed have no effect. If no locale has been specified, the container's default locale is returned.

See Also:
setLocale(java.util.Locale)


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!