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("<font color='red'>你好,world2!</font>"); %>
    <% out.write("<font color='green'>你好,world3!</font>"); %>
浏览器输出结果:
JSP中out.write()和out.print()的区别

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

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

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

    <% out.write("<font color='green'>你好,world3!<br></font>"); %>


out.println()输出到客户端。
    在out.println()中,out是response的实例,是以response为对象进行流输出的,即将内容输出到客户端。如果在JSP页面中使用System.out.println(),在客户端只会输出一个空格。


System.out.println()打印在控制台当中。
    System.out.println()用的是标准输出流,这个是输出在控制台上的,而JSP不是控制台程序。不管是在JSP还是在JAVA程序中,System.out.println()都是打印在控制台上。 如果想打印在页面,简单点的方法是:
out.print( "要打印的内容" ); 

    其实在正规的网站建设中,是从来不用out.println()的,都是直接使用标签。

例:
服务器平台:tomcat。
客户端:firefox浏览器。
源程序:
//test.jsp文件
<%@page contentType="text/html;charset=gb2312"%>
<html>
<body>
 
<%@ page import = "Java.util.Date"%>
<%
out.println("This is printed by out.println.");
System.out.println("This is printed by System.out.println.");
System.out.println("This is printed by System.out.println.");
System.out.println("This is printed by System.out.println.");
out.println("This is printed by out.println.");


%> 
</body>
</html>


客户端(浏览器)中的结果:




从上图中可看出两个out.println()输出的内容间有一个空格(尽管源程序调用了3次System.out.println)。


控制台中的结果:




从上图可看到调用3次System.out.println()的内容(其余的是服务器信息 )。


posted on 2016-07-13 06:39  波波嘟  阅读(620)  评论(0)    收藏  举报