JSP中out.write()和out.prin…

来源:互联网 发布:住建部资质申报软件 编辑:程序博客网 时间:2024/06/05 21:18
原文地址:JSP中out.write()和out.print()的区别作者:学无止境
out对象的类型是JspWriter。JspWriter继承了java.io.Writer类。

1)print方法是子类JspWriter,write是Writer类中定义的方法;

2)重载的print方法可将各种类型的数据转换成字符串的形式输出,而重载的write方法只能输出字符、字符数组和字符串等与字符相关的数据;

3)JspWriter类型的out对象使用print方法和write方法都可以输出字符串,但是,如果字符串对象的值为null时,print方法将输出内容为“null”的字符串,而write方法则是抛出NullPointerException异常。例如:

下面的test.jsp文件:

<% String str=null;

out.print(str);

//out.write(str);

%>
##################################
示例一、
   <% out.print("<fontcolor='red'>你好,world2!</font>");%>
   <% out.write("<fontcolor='green'>你好,world3!</font>");%>
浏览器输出结果:
[转载]JSP中out.write()和out.print()的区别

查看浏览器源代码:
[转载]JSP中out.write()和out.print()的区别

示例二、
   <% out.println("<fontcolor='red'>你好,world2!</font>");%>
   <% out.write("<fontcolor='green'>你好,world3!</font>");%>
浏览器输出结果:(和示例一相同)
[转载]JSP中out.write()和out.print()的区别
浏览器源代码:(和示例一相比 源代码换行了)
[转载]JSP中out.write()和out.print()的区别

################################################################
另外值得注意的是:
     没有out.writeln()这个函数;要想显示在浏览器上的结果换行,可以加上<br>
如:  <%out.println("<fontcolor='red'>你好,world2!<br></font>");%>
   <% out.write("<fontcolor='green'>你好,world3!<br></font>");%>



原创粉丝点击