public abstract void clearBuffer() :清除缓冲区的数据,如果在flush之后调用不会抛出异常。
public abstract void clear() :清除缓冲区的当前内容,如果在flush之后调用会抛出异常。
1. clear()测试
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Out</title>
</head>
<body>
<%
out.write("hello ");
out.write("world");
out.flush();
out.clear();
out.write("!!!");
System.out.println(out.getRemaining());
%>
</body>
</html>
结果:
控制台无输出,客户端数据不显示!(抛异常或者卡住)
2. clearBuffer()测试
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Out</title>
</head>
<body>
<%
out.write("hello ");
out.write("world");
out.flush();
out.clearBuffer();
out.write("!!!");
System.out.println(out.getRemaining());
%>
</body>
</html>
结果:
控制台有输出,客户端数据显示!
3. 单独测试
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Out</title>
</head>
<body>
<%
out.write("hello ");
out.write("world");
out.clearBuffer();
System.out.println(out.isAutoFlush());
%>
</body>
</html>
结果
单独使用clear()或者clearBuffer()方法,客户端数据都不显示,但控制台有输出!