对于所有你“重复”的狂热分子,在SO右here上有类似的问题。不同的是,我画了一个生动的例子,我无法理解输出。Java EE中JspWriter和PrintWriter的区别?
为JspWriter和PrintWriter文档说有两点不同:的JspWriter可以抛出异常,PrintWriter的不应该这样做。 2. JspWriter在场景后面使用PrintWriter,但是由于默认情况下JSP页面被缓冲,所以直到the buffer is flushed
才会创建PrintWriter - 无论在JSP页面的上下文中是什么意思。我不确定我是否理解了这最后一部分。考虑这个JSP页面:
<%@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在将所有其他内容发送到浏览器之前输出其字符串。如果我们检查发送给浏览器的源代码,则在DOCTYPE声明之前,PrintWriter的字符串将作为第一行发送。所以在上面的例子中,究竟发生了什么?
呃你走了!谢谢你的队友,非常感谢。 –