JspWriter (Java EE 5)

Java EE API


javax.servlet.jsp Class JspWriter

java.lang.Object
  extended by java.io.Writer
      extended by javax.servlet.jsp.JspWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable
Direct Known Subclasses:
BodyContent

public abstract class JspWriter
extends Writer

Extends: java.io.Writer
Extended by: BodyContent

JSP 页面中的操作和模板数据是使用 JspWriter 对象编写的,该对象由隐式变量 out 引用,而此变量是使用 PageContext 对象中的方法自动初始化的。

此抽象类模仿 java.io.BufferedWriter 和 java.io.PrintWriter 类中的一些功能,但又不同于这些类,因为它从 print 方法抛出 java.io.IOException,而 PrintWriter 不会这样做。

缓冲

初始 JspWriter 对象与 ServletResponse 的 PrintWriter 对象关联,关联方式取决于页面是否被缓冲。如果页面未被缓冲,则写入此 JspWriter 对象的输出将直接写入 PrintWriter,如有必要,将通过对响应对象调用 getWriter() 方法创建 PrintWriter。但是,如果页面被缓冲,则在刷新缓冲区之前不会创建 PrintWriter 对象,并且 setContentType() 之类的操作是合法的。因为这种灵活性显着简化了编程,所以默认情况下将对 JSP 页面进行缓冲。

缓冲会带来超出缓冲区时该如何处理的问题。可以采用两种方法:

  • 超出缓冲区不是致命错误;在超出缓冲区时,只需刷新输出即可。
  • 超出缓冲区是致命错误;在超出缓冲区时,引发一个异常。

两种方法都有效,并且在 JSP 技术中都得到支持。页面的行为由 autoFlush 属性控制,该属性的默认值为 true。通常,需要确保已将正确且完整的数据发送到其客户端的 JSP 页面可能想将 autoFlush 设置为 false,比如客户端是应用程序自身的情况。另一方面,发送有意义的数据(即使只构造了一部分时)的 JSP 页面可能想将 autoFlush 设置为 true;比如发送要通过浏览器立即显示的数据时。每个应用程序都需要考虑自己的特定需求。

一个可以考虑的选择是不设置缓冲区大小界限;但是,这又带来了计算不受控制的弊病,将消耗数量无法估计的资源。

JSP 实现类的 "out" 隐式变量就属于这种类型。如果页面指令选择 autoflush="true",那么在导致溢出情况时,或者在当前操作是在没有进行刷新的情况下执行的时候,此类上的所有 I/O 操作都将自动刷新缓冲区的内容。如果 autoflush="false",那么在执行当前操作将导致缓冲区溢出的情况下,此类上的所有 I/O 操作都将抛出 IOException。

英文文档:

The actions and template data in a JSP page is written using the JspWriter object that is referenced by the implicit variable out which is initialized automatically using methods in the PageContext object.

This abstract class emulates some of the functionality found in the java.io.BufferedWriter and java.io.PrintWriter classes, however it differs in that it throws java.io.IOException from the print methods while PrintWriter does not.

Buffering

The initial JspWriter object is associated with the PrintWriter object of the ServletResponse in a way that depends on whether the page is or is not buffered. If the page is not buffered, output written to this JspWriter object will be written through to the PrintWriter directly, which will be created if necessary by invoking the getWriter() method on the response object. But if the page is buffered, the PrintWriter object will not be created until the buffer is flushed and operations like setContentType() are legal. Since this flexibility simplifies programming substantially, buffering is the default for JSP pages.

Buffering raises the issue of what to do when the buffer is exceeded. Two approaches can be taken:

  • Exceeding the buffer is not a fatal error; when the buffer is exceeded, just flush the output.
  • Exceeding the buffer is a fatal error; when the buffer is exceeded, raise an exception.

Both approaches are valid, and thus both are supported in the JSP technology. The behavior of a page is controlled by the autoFlush attribute, which defaults to true. In general, JSP pages that need to be sure that correct and complete data has been sent to their client may want to set autoFlush to false, with a typical case being that where the client is an application itself. On the other hand, JSP pages that send data that is meaningful even when partially constructed may want to set autoFlush to true; such as when the data is sent for immediate display through a browser. Each application will need to consider their specific needs.

An alternative considered was to make the buffer size unbounded; but, this had the disadvantage that runaway computations would consume an unbounded amount of resources.

The "out" implicit variable of a JSP implementation class is of this type. If the page directive selects autoflush="true" then all the I/O operations on this class shall automatically flush the contents of the buffer if an overflow condition would result if the current operation were performed without a flush. If autoflush="false" then all the I/O operations on this class shall throw an IOException if performing the current operation would result in a buffer overflow condition.

See Also:
Writer, BufferedWriter, PrintWriter

Field Summary
protected  boolean
protected  int
static int
static int
static int
 
Fields inherited from class java.io.Writer
 
Constructor Summary
protected
 
Method Summary
abstract  void
abstract  void
abstract  void
abstract  void
 int
abstract  int
 boolean
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
 
Methods inherited from class java.io.Writer
 
Methods inherited from class java.lang.Object
 

Field Detail

英文文档:

NO_BUFFER

public static final int NO_BUFFER
Constant indicating that the Writer is not buffering output.

See Also:
Constant Field Values


英文文档:

DEFAULT_BUFFER

public static final int DEFAULT_BUFFER
Constant indicating that the Writer is buffered and is using the implementation default buffer size.

See Also:
Constant Field Values


英文文档:

UNBOUNDED_BUFFER

public static final int UNBOUNDED_BUFFER
Constant indicating that the Writer is buffered and is unbounded; this is used in BodyContent.

See Also:
Constant Field Values


英文文档:

bufferSize

protected int bufferSize
The size of the buffer used by the JspWriter.


英文文档:

autoFlush

protected boolean autoFlush
Whether the JspWriter is autoflushing.

Constructor Detail

protected JspWriter(int bufferSize, boolean autoFlush)
受保护的构造方法。
bufferSize 将由 JspWriter 使用的缓冲区的大小
autoFlush JspWriter 是否应自动刷新
英文文档:

JspWriter

protected JspWriter(int bufferSize,
                    boolean autoFlush)
Protected constructor.

Parameters:
bufferSize - the size of the buffer to be used by the JspWriter
autoFlush - whether the JspWriter should be autoflushing

Method Detail

abstract public void newLine() throws java.io.IOException
写入一个行分隔符。行分隔符字符串由系统属性 line.separator 定义,并且不一定是单个换行 ('\n') 字符。
Throwsjava.io.IOException: 如果发生 I/O 错误
英文文档:

newLine

public abstract void newLine()
                      throws IOException
Write a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.

Throws:
IOException - If an I/O error occurs

abstract public void print(boolean b) throws java.io.IOException
打印 boolean 值。将 valueOf(boolean) 生成的字符串写入 JspWriter 的缓冲区,如果没有使用缓冲区,则直接将该字符串写入底层 Writer。
b 要打印的 boolean
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

print

public abstract void print(boolean b)
                    throws IOException
Print a boolean value. The string produced by String.valueOf(boolean) is written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Parameters:
b - The boolean to be printed
Throws:
IOException - If an error occured while writing

abstract public void print(char c) throws java.io.IOException
打印字符。将字符写入 JspWriter 的缓冲区,如果没有使用缓冲区,则直接将字符写入底层 Writer。
c 要打印的 char
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

print

public abstract void print(char c)
                    throws IOException
Print a character. The character is written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Parameters:
c - The char to be printed
Throws:
IOException - If an error occured while writing

abstract public void print(int i) throws java.io.IOException
打印整数。将 valueOf(int) 生成的字符串写入 JspWriter 的缓冲区,如果没有使用缓冲区,则直接将该字符串写入底层 Writer。
i 要打印的 int
Throwsjava.io.IOException: 如果写入时发生错误
See also toString(int)
英文文档:

print

public abstract void print(int i)
                    throws IOException
Print an integer. The string produced by String.valueOf(int) is written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Parameters:
i - The int to be printed
Throws:
IOException - If an error occured while writing
See Also:
Integer.toString(int)

abstract public void print(long l) throws java.io.IOException
打印 long 整数。将 valueOf(long) 生成的字符串写入 JspWriter 的缓冲区,如果没有使用缓冲区,则直接将该字符串写入底层 Writer。
l 要打印的 long
Throwsjava.io.IOException: 如果写入时发生错误
See also toString(long)
英文文档:

print

public abstract void print(long l)
                    throws IOException
Print a long integer. The string produced by String.valueOf(long) is written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Parameters:
l - The long to be printed
Throws:
IOException - If an error occured while writing
See Also:
Long.toString(long)

abstract public void print(float f) throws java.io.IOException
打印浮点数。将 valueOf(float) 生成的字符串写入 JspWriter 的缓冲区,如果没有使用缓冲区,则直接将该字符串写入底层 Writer。
f 要打印的 float
Throwsjava.io.IOException: 如果写入时发生错误
See also toString(float)
英文文档:

print

public abstract void print(float f)
                    throws IOException
Print a floating-point number. The string produced by String.valueOf(float) is written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Parameters:
f - The float to be printed
Throws:
IOException - If an error occured while writing
See Also:
Float.toString(float)

abstract public void print(double d) throws java.io.IOException
打印双精度浮点数。将 valueOf(double) 生成的字符串写入 JspWriter 的缓冲区,如果没有使用缓冲区,则直接将该字符串写入底层 Writer。
d 要打印的 double
Throwsjava.io.IOException: 如果写入时发生错误
See also toString(double)
英文文档:

print

public abstract void print(double d)
                    throws IOException
Print a double-precision floating-point number. The string produced by String.valueOf(double) is written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Parameters:
d - The double to be printed
Throws:
IOException - If an error occured while writing
See Also:
Double.toString(double)

abstract public void print(char[] s) throws java.io.IOException
打印字符数组。将字符写入 JspWriter 的缓冲区,如果没有使用缓冲区,则直接将字符写入底层 Writer。
s 要打印的字符数组
ThrowsNullPointerException: 如果 snull
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

print

public abstract void print(char[] s)
                    throws IOException
Print an array of characters. The characters are written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Parameters:
s - The array of chars to be printed
Throws:
NullPointerException - If s is null
IOException - If an error occured while writing

abstract public void print(String s) throws java.io.IOException
打印字符串。如果参数为 null,则打印字符串 "null"。否则,将字符串的字符写入 JspWriter 的缓冲区,如果没有使用缓冲区,则直接将字符写入底层 Writer。
s 要打印的 String
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

print

public abstract void print(String s)
                    throws IOException
Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Parameters:
s - The String to be printed
Throws:
IOException - If an error occured while writing

abstract public void print(Object obj) throws java.io.IOException
打印对象。将 valueOf(Object) 方法生成的字符串写入 JspWriter 的缓冲区,如果没有使用缓冲区,则直接将该字符串写入底层 Writer。
obj 要打印的 Object
Throwsjava.io.IOException: 如果写入时发生错误
See also toString()
英文文档:

print

public abstract void print(Object obj)
                    throws IOException
Print an object. The string produced by the String.valueOf(Object) method is written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Parameters:
obj - The Object to be printed
Throws:
IOException - If an error occured while writing
See Also:
Object.toString()

abstract public void println() throws java.io.IOException
通过写入行分隔符字符串终止当前行。行分隔符字符串由系统属性 line.separator 定义,不一定是单个换行符 ('\n')。
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println()
                      throws IOException
Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

Throws:
IOException - If an error occured while writing

abstract public void println(boolean x) throws java.io.IOException
打印 boolean 值,然后终止该行。此方法的行为就像先调用 #print(boolean) 然后调用 #println() 一样。
x 要写入的 boolean
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println(boolean x)
                      throws IOException
Print a boolean value and then terminate the line. This method behaves as though it invokes print(boolean) and then println().

Parameters:
x - the boolean to write
Throws:
IOException - If an error occured while writing

abstract public void println(char x) throws java.io.IOException
打印字符,然后终止该行。此方法的行为就像先调用 #print(char) 然后调用 #println() 一样。
x 要写入的字符
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println(char x)
                      throws IOException
Print a character and then terminate the line. This method behaves as though it invokes print(char) and then println().

Parameters:
x - the char to write
Throws:
IOException - If an error occured while writing

abstract public void println(int x) throws java.io.IOException
打印整数,然后终止该行。此方法的行为就像先调用 #print(int) 然后调用 #println() 一样。
x 要写入的 int
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println(int x)
                      throws IOException
Print an integer and then terminate the line. This method behaves as though it invokes print(int) and then println().

Parameters:
x - the int to write
Throws:
IOException - If an error occured while writing

abstract public void println(long x) throws java.io.IOException
打印 long 整数,然后终止该行。此方法的行为就像先调用 #print(long) 然后调用 #println() 一样。
x 要写入的 long
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println(long x)
                      throws IOException
Print a long integer and then terminate the line. This method behaves as though it invokes print(long) and then println().

Parameters:
x - the long to write
Throws:
IOException - If an error occured while writing

abstract public void println(float x) throws java.io.IOException
打印浮点数,然后终止该行。此方法的行为就像先调用 #print(float) 然后调用 #println() 一样。
x 要写入的 float
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println(float x)
                      throws IOException
Print a floating-point number and then terminate the line. This method behaves as though it invokes print(float) and then println().

Parameters:
x - the float to write
Throws:
IOException - If an error occured while writing

abstract public void println(double x) throws java.io.IOException
打印双精度浮点数,然后终止该行。此方法的行为就像先调用 #print(double) 然后调用 #println() 一样。
x 要写入的 double
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println(double x)
                      throws IOException
Print a double-precision floating-point number and then terminate the line. This method behaves as though it invokes print(double) and then println().

Parameters:
x - the double to write
Throws:
IOException - If an error occured while writing

abstract public void println(char[] x) throws java.io.IOException
打印字符数组,然后终止该行。此方法的行为就像先调用 print(char[]) 然后调用 println() 一样。
x 要写入的 char[]
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println(char[] x)
                      throws IOException
Print an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().

Parameters:
x - the char[] to write
Throws:
IOException - If an error occured while writing

abstract public void println(String x) throws java.io.IOException
打印 String,然后终止该行。此方法的行为就像先调用 #print(String) 然后调用 #println() 一样。
x 要写入的 String
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println(String x)
                      throws IOException
Print a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

Parameters:
x - the String to write
Throws:
IOException - If an error occured while writing

abstract public void println(Object x) throws java.io.IOException
打印 Object,然后终止该行。此方法的行为就像先调用 #print(Object) 然后调用 #println() 一样。
x 要写入的 Object
Throwsjava.io.IOException: 如果写入时发生错误
英文文档:

println

public abstract void println(Object x)
                      throws IOException
Print an Object and then terminate the line. This method behaves as though it invokes print(Object) and then println().

Parameters:
x - the Object to write
Throws:
IOException - If an error occured while writing

abstract public void clear() throws java.io.IOException
清除缓冲区的内容。如果已经刷新缓冲区,则 clear 操作将抛出 IOException,指示某些数据已经不可撤消地写入了客户端响应流。
Throwsjava.io.IOException: 如果发生 I/O 错误
英文文档:

clear

public abstract void clear()
                    throws IOException
Clear the contents of the buffer. If the buffer has been already been flushed then the clear operation shall throw an IOException to signal the fact that some data has already been irrevocably written to the client response stream.

Throws:
IOException - If an I/O error occurs

abstract public void clearBuffer() throws java.io.IOException
清除缓冲区的当前内容。与 clear() 不同,如果已经刷新缓冲区,此方法不会抛出 IOException。它只清除缓冲区的当前内容,然后返回。
Throwsjava.io.IOException: 如果发生 I/O 错误
英文文档:

clearBuffer

public abstract void clearBuffer()
                          throws IOException
Clears the current contents of the buffer. Unlike clear(), this method will not throw an IOException if the buffer has already been flushed. It merely clears the current content of the buffer and returns.

Throws:
IOException - If an I/O error occurs

abstract public void flush() throws java.io.IOException
刷新流。如果该流已保存缓冲区中各种 write() 方法的所有字符,则立即将它们写入预期目标。然后,如果该目标是另一个字符或字节流,则将其刷新。因此,一次 flush() 调用将刷新 Writer 和 OutputStream 链中的所有缓冲区。

如果超出缓冲区大小,则可以间接调用该方法。

在关闭流之后,再调用 write() 或 flush() 将导致抛出 IOException。

Throwsjava.io.IOException: 如果发生 I/O 错误

英文文档:

flush

public abstract void flush()
                    throws IOException
Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

The method may be invoked indirectly if the buffer size is exceeded.

Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown.

Specified by:
flush in interface Flushable
Specified by:
flush in class Writer
Throws:
IOException - If an I/O error occurs

abstract public void close() throws java.io.IOException
关闭流,先将它刷新。

不必对初始 JspWriter 显式调用此方法,因为 JSP 容器生成的代码将自动包含对 close() 的调用。

与 flush() 不同,关闭以前关闭的流无效。

Throwsjava.io.IOException: 如果发生 I/O 错误

英文文档:

close

public abstract void close()
                    throws IOException
Close the stream, flushing it first.

This method needs not be invoked explicitly for the initial JspWriter as the code generated by the JSP container will automatically include a call to close().

Closing a previously-closed stream, unlike flush(), has no effect.

Specified by:
close in interface Closeable
Specified by:
close in class Writer
Throws:
IOException - If an I/O error occurs

public int getBufferSize()
此方法返回 JspWriter 使用的缓冲区的大小。
return 缓冲区的大小(以字节为单位),如果未被缓冲,则返回 0。
英文文档:

getBufferSize

public int getBufferSize()
This method returns the size of the buffer used by the JspWriter.

Returns:
the size of the buffer in bytes, or 0 is unbuffered.

abstract public int getRemaining()
此方法返回缓冲区中未使用字节的数量。
return 缓冲区中未使用字节的数量
英文文档:

getRemaining

public abstract int getRemaining()
This method returns the number of unused bytes in the buffer.

Returns:
the number of bytes unused in the buffer

public boolean isAutoFlush()
此方法指示 JspWriter 是否将自动刷新。
return 此 JspWriter 是否将自动刷新,在发生缓冲区溢出的情况下抛出 IOException
英文文档:

isAutoFlush

public boolean isAutoFlush()
This method indicates whether the JspWriter is autoFlushing.

Returns:
if this JspWriter is auto flushing or throwing IOExceptions on buffer overflow conditions


Submit a bug or feature

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

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

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