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