Java EE中JspWriter和PrintWriter的区别? [英] Difference between JspWriter and PrintWriter in Java EE?

查看:198
本文介绍了Java EE中JspWriter和PrintWriter的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于所有复制的狂热分子,SO上有一个类似的问题这里。不同的是,我画了一个生动的例子,我无法理解输出。

For all you "duplicate" fanatics, there is a similar question on SO right here. The difference is that I paint a vivid example that I can not understand the output of.

JspWriter PrintWriter 说有两点不同: 1. JspWriter可以抛出异常,PrintWriter不应该这样做。 2. JspWriter在场景后面使用PrintWriter,但由于默认情况下JSP页面是缓冲的,因此只有在刷新缓冲区时才会创建PrintWriter - JSP页面上下文中的含义。我不确定我是否理解了最后一部分。考虑这个JSP页面:

The documentation for JspWriter and PrintWriter says there are two differences: 1. JspWriter can throw exceptions, PrintWriter should not do so. 2. JspWriter uses a PrintWriter behind the scene, but since by default JSP pages are buffered, the PrintWriter won't be created until the buffer is flushed - whatever that mean in the context of a JSP page. I'm not sure I've understood this last part. Consider this JSP page:

<%@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的字符串将作为第一行发送。那么在上面的例子中,到底发生了什么?

As you can see, the JspWriter outputs his string to the browser were I expected it to. But PrintWriter outputs his string before all else is sent to the browser. If we examine the source code sent to the browser, the PrintWriter's string is sent as the very first line, before the DOCTYPE declaration. So in the example above, exactly what happens?

推荐答案

解释在你自己的问题中:

The explanation is in your own question:


JspWriter在场景后面使用PrintWriter,但由于默认情况下
JSP页面被缓冲,因此在
缓冲区之前不会创建PrintWriter刷新

JspWriter uses a PrintWriter behind the scene, but since by default JSP pages are buffered, the PrintWriter won't be created until the buffer is flushed

这意味着写入JspWriter的内容被缓冲,并且刷新此缓冲区后(因为缓冲区已满) ,或者因为JSP已经到了执行的末尾),内容被写入响应的PrintWriter。

This means that what is written to the JspWriter is buffered, and once this buffer is flushed (either because the buffer is full, or because the JSP has reached the end of its execution), the contents is written to the response's PrintWriter.

所以你的例子的流程如下:

So the flow of your example is the following one:


  • 静态HTML代码,直到scriptlet:写入内存缓冲区

  • out.println( ...):写入内存缓冲区

  • pw.println(...):写入响应

  • 静态HTML代码直到JSP结束:写入内存中ffer

  • 刷新内存缓冲区:它包含的所有内容都写入响应

  • static HTML code until the scriptlet: written to the in-memory buffer
  • out.println(...): written to the in-memory buffer
  • pw.println(...): written to the response
  • static HTML code until the end of the JSP: written to the in-memory buffer
  • flush of the in-memory buffer: all it contains is written to the response

这篇关于Java EE中JspWriter和PrintWriter的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆