🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# JSP 中的`out`隐式对象 > 原文: [https://beginnersbook.com/2013/11/jsp-implicit-object-out-with-examples/](https://beginnersbook.com/2013/11/jsp-implicit-object-out-with-examples/) 它是`javax.servlet.jsp.JspWriter`的一个实例。这允许我们访问 Servlet 输出流。需要发送到客户端(浏览器)的输出通过此对象传递。简单来说,使用隐式对象将内容写入客户端。 ### `out`隐式对象的方法 ```java void print() void println() void newLine() void clear() void clearBuffer() void flush() boolean isAutoFlush() int getBufferSize() int getRemaining() ``` 让我们详细看看每个方法: 1. `void print()`:此方法写入已传递给它的值。对于例如下面的语句将在 jSP 中显示一个句子`Out Implicit Object in jSP - BeginnersBook`到输出屏幕(客户端浏览器)。 ```html out.print(“Out Implicit Object in jSP - BeginnersBook”); ``` 2. `void println()`:此方法类似于`print()`方法,`print`和`println`之间的唯一区别是`println()`方法在末尾添加了新行字符。让我们借助示例来了解差异。 **`print`:** ```html out.print(“hi”); out.print(" "); out.print(“hello”); ``` 浏览器输出:所有 3 个`out.print`语句的结果之间不会有新的界限。 ```java hi hello ``` **`println`:** ```html out.println(“hi”); out.println(“hello”); ``` 浏览器输出: ```java hi hello ``` 3. `void newLine()`:此方法将新行添加到输出。 示例 – ```html out.print(“This will write content without a new line”); out.newLine(); out.print(“I’m just an another print statement”); ``` **输出:** ```java This will write content without a new line I’m just an another print statement ``` 如您所知,`print`语句不会添加新行。我们使用`newLine()`方法在两个`out.print`语句之间添加了一个新行。 4. `void clear()`:它清除输出缓冲区,甚至不让它将缓冲区内容写入客户端。这就是它的名称: ```html out.clear(); ``` 5. `void clearBuffer()`:此方法类似于`clear()`方法。它们之间的唯一区别是,当我们在已经刷新的缓冲区上调用`out.clear()`时会抛出异常,但`out.clearBuffer()`却没有。 6. `void flush()`:这个方法也像`clear()`方法一样清除缓冲区但强制它在刷新之前将内容写入输出,这意味着缓冲区中的任何内容都会被写入清除缓冲区之前的客户端屏幕。 7. `boolean isAutoFlush()`:返回布尔值`true/false`。它用于检查缓冲区是否自动刷新。 8. `int getBufferSize()`:此方法以字节为单位返回输出缓冲区的大小。 9. `int getRemaining()`:它返回在达到缓冲区溢出条件之前剩余的字节数。 ### `out`隐式对象示例 在这个例子中,我们使用`out`的`print`和`println`方法向客户端显示少量消息。 `index.jsp` ```html <HTML> <HEAD> <TITLE> OUT IMPLICIT OBJECT EXAMPLE </TITLE> </HEAD> <BODY> <% out.print( "print statement " ); out.println( "println" ); out.print("Another print statement"); %> </BODY> </HTML> ``` **输出:** ```java print statement println Another print statement ```