zoukankan      html  css  js  c++  java
  • JspWriter与PrintWriter(转)

    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类型对象做的

  • 相关阅读:
    axios的拦截请求与响应
    Vue.mixin言简意赅的示例
    vue的formdata图片预览以及上传
    vue页面更新数据
    vue对象比较,阻止后退
    vue检索内容
    vue侧滑菜单
    JavaScript易错知识点整理
    写好你的JavaScript
    LeetCode123 Best Time to Buy and Sell Stock III
  • 原文地址:https://www.cnblogs.com/liu-Gray/p/4911752.html
Copyright © 2011-2022 走看看