JSP中out.write()和out.print()的区别
2016-07-13 06:39
423 查看
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>"); %>
浏览器输出结果:
![](http://s6.sinaimg.cn/middle/7ffb8dd5gc93dd5e33795&690)
查看浏览器源代码:
![](http://s6.sinaimg.cn/middle/7ffb8dd5gc93dd8736cd5&690)
示例二、
<%
out.println("<font color='red'>你好,world2!</font>"); %>
<%
out.write("<font color='green'>你好,world3!</font>"); %>
浏览器输出结果:(和示例一相同)
![](http://s4.sinaimg.cn/middle/7ffb8dd5gc93ddd63da63&690)
浏览器源代码:(和示例一相比 源代码换行了)
![](http://s10.sinaimg.cn/middle/7ffb8dd5gc93de4407d39&690)
################################################################
另外值得注意的是:
没有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>
客户端(浏览器)中的结果:
![](https://img-blog.csdn.net/20130514231350263)
从上图中可看出两个out.println()输出的内容间有一个空格(尽管源程序调用了3次System.out.println)。
控制台中的结果:
![](https://img-blog.csdn.net/20130514231405731)
从上图可看到调用3次System.out.println()的内容(其余的是服务器信息 )。
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>"); %>
浏览器输出结果:
查看浏览器源代码:
示例二、
<%
out.println("<font color='red'>你好,world2!</font>"); %>
<%
out.write("<font color='green'>你好,world3!</font>"); %>
浏览器输出结果:(和示例一相同)
浏览器源代码:(和示例一相比 源代码换行了)
################################################################
另外值得注意的是:
没有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()的内容(其余的是服务器信息 )。
相关文章推荐
- JSP中out.write()和out.print()的区别
- JSP中out.write()和out.print()的区别
- Three.js学习笔记 – “我和小伙伴都惊呆了”的特效和Three.js初探
- 20个令人印象深刻的例子学习Three.js实现WebGL
- 解决js中文输入法下keyCode总为229的问题
- JavaScript闭包(Closure)学习笔记
- JS定时器案列图片轮播
- JavaScript中的函数的参数(简单类型,引用类型)+图解
- 【JavaScript高级程序设计】--第2章 在HTML中使用JavaScript
- JavaScript笔记
- 现代浏览器JS异步加载方案
- jsonModel解释驼峰和下划线数据
- javascript基础知识------定义函数
- javascript的3种输出方式
- javascript中的警告对话框
- 【JavaScript】——匿名函数和闭包
- JavaScript回调函数
- JavaScript Array 原型方法 大盘点
- 主页面调用iframe里面匿名Javascript函数的问题
- javascript闭包,作用域,自调用匿名函数