今天查资料的时候看到了jspwriter和printwriter的区别, 但是其中的一部分不是很理解.
<%@page import="java.io.PrintWriter"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JspWriter versus PrintWriter</title>
</head>
<body>
<p>I should be row one.</p>
<%
out.println("<p>JspWriter said: I should be the second row.</p>");
PrintWriter pw = response.getWriter();
pw.println("<p>PrintWriter said: I should be the third row.</p>");
%>
<p>I should be the fourth row.</p>
</body>
</html>
输出如下:
PrintWriter said: I should be the third row.
I should be row one.
JspWriter said: I should be the second row.
I should be the fourth row.
后来查了一下, 几乎所有的博客都是这么解释的:
不管JspWriter与PrintWriter在程序中的顺序怎么样,始终先会输出PringWriter中的数据然后再输出JspWriter中的数据.这是因为jsp内置的out对象相当于插入到了PrintWriter前面的缓冲区中.out对象满足一定条件时,才会调用PrintWriter对象的print()方法,把out缓冲区中的内容输出到浏览器端
我想了1个多小时,也自己动手试了一下,得出的结果就是
JspWriter
在jsp页面
设置允许缓冲,是使用jsp内置的缓冲区的, 但是通过response.getwriter
得到的writer
是不使用内置的缓冲区,直接将内容写到网页中
不知道这么想对不对, 还望大家指正