首页 > Web开发 > 详细

JspWriter与PrintWriter(转)

时间:2015-10-26 18:31:18      阅读:351      评论:0      收藏:0      [点我收藏+]

1都继承自java.io.writer类

 JspWriter在Jsp页面上直接用out对象输出,也可以直接用pageContex.getOut()得到out对象

 PrintWriter必须通过response.getwriter()得到

 2.在Jsp页面上用两种方法同时输出数据,PrintWriter中的数据会先输出

 例如:

新建test.jsp页面

<%
out.println("out");
JspWriter out1 = pageContext.getOut();
if(out == out1 )
{
 out.println("out==out1");
}
else
{
 out.println("out!=out1");
}

PrintWriter pw = response.getWriter();
pw.write("pw writer");
 %>

运行结果为

pw writer out out==out1 This is my JSP page.

原因:

out对象相当于插入到了PrintWriter前面的缓冲区中.out对象满足一定条件时

,才会调用PrintWriter对象的print()方法,把out缓冲区中的内容输出到浏览器端

如果想让上面的代码的按代码的先后顺序输出可以写成:

out.flush();

刷新缓存区即可

则输出结果变为

out out==out1 pw writer This is my JSP page

 

 

另外:PrintWriter的print方法中不会抛出IOException,而JspWriter会。
JspWriter是抽象类而PrintWriter不是,也就是说你可以通过new操作来直接新建一个PrintWriter的对象而JspWriter不行,它必须是通过其子类来新建。
但是它们之间又是有关系的,这个关系主要是JspWriter对PrintWriter有依赖。初始化一个JspWriter对象的时候要关联ServletResponse对象的一个PrintWriter类型对象,最终JspWriter对象的输出任务还是通过这个PrintWriter类型对象做的

JspWriter与PrintWriter(转)

原文:http://www.cnblogs.com/liu-Gray/p/4911752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!