`

out.print和out.write

    博客分类:
  • JSP
阅读更多
问题:

这是一个JSP页面:
<%@ page language="java" import="java.util.*"  %>
<%@ page pageEncoding="UTF-8" isELIgnored="false" %>
<%@ page import="java.util.Date" %>

<html>
  <head> 
    <title>  </title>
  </head>  
  <body>
 	<h1>现在时间:</h1>
 	<%=new Date() %>
  </body>
</html>

转译后生成的servlet,对应的body中,有以下代码:
out.write("<h1>现在时间:</h1>\n");
out.print( new Date());


思考:
<%=  %>表达式代表的是什么? 为什么 new Date()后,没有 “;”(分号)?
servlet中,out.write() 和 out.print() 有何区别?

解答:
There is a method print(Object). There is no method write(Object)
write(String) exists however.
serch the javaAPI you will find
------------------------------------------
(java中)有这个方法:print(Object),没有这个方法:write(Object)
但是却有这个方法:write(String)
查找javaAPI,你便会发现。

总结:
out.print()方法,可以输出一个java对象;
out.write()方法,只能输入一个字符串。


参考资料:
jsp中的out对象是JspWriter类型的.而JspWriter继承了java.io.Writer .

out.write() 是在在父类Writer中定义的
out.print() 是在子类JspWriter中定义的.

重写的 print 方法可以将各种类型的数据转换成字符串的形式输出.
而重载的 write 方法只能输出字符/字符数组/字符串等与字符相关的数据.

而且如果使用这两种方法输出值为null的字符串对象,
那么print方法输出的结果是"null",
而write方法则会抛出NullPoiterException异常.



-
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/1757126








引用:
http://bbs.csdn.net/topics/300007142
http://wenwen.soso.com/z/q152174496.htm





--
分享到:
评论

相关推荐

    深入浅析Jsp中 out.print 和 out.write 的区别

    jsp中的out对象是JspWriter类型的. 而JspWriter继承了java.io.Writer . write方法是在在父类Writer中定义的, print方法是在子类JspWriter中定义...out.write()是字节输出流的方法 out.print()是字符输出流的方法 在j

    java中out.print和out.write的方法

    本文用一个小例子说明java out.print和out.write的方法,大家参考使用吧

    桌面管家源码(java)

    System.out.print("create fail!"); } } public void Ccreateconnect(String ip) { try { socket=new Socket(ip,9999);//创建套接字 } catch(Exception e) { System.out.print("创立连接失败!"); } ...

    关于Ajax技术中servlet末尾的输出流

    Ajax的服务器端用PrintWriter out=resp.getWriter()来响应数据的时候,out.print(0)、out.print(1)来表示成功或失败,而不用out.write是有原因的,首先来看一下print和write两者的异同点。  共同点是:两者都不刷新...

    最新JAVA编程题全集_50题及答案

    System.out.print("a="+a+" b="+b); 折半查找 public class Test { public static int[] data = { 12, 15, 20, 10, 19, 3, 89, 32, 39, 47, 55 }; // 原始数据 public static int counter = 1; // 计数器 ...

    人工智能-项目实践-搜索引擎-java实验1-实现搜索引擎的倒排索引数据结构

    如果写文本文件,推荐使用PrintWriter,当创建好PrintWriter对象后,调用其println和print方法可以将字符串一行行的写入到文本文件,使用方法与System.out.println, System.out.print完全一样 具体使用方法,请见...

    javazhuabaoshili.rar

    Java抓包工具及测试实例源码package netPackage; /******************* * JpcapTip.java */ //import java.net.NetworkInterface; //import java.net.InetAddress; //import java.net.... System.out.print

    csv文件读下用的jar包

    System.out.print("--" + s); } System.out.println(); } } catch (IOException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } ...

    MTA-Python-模拟考试题库及答案.docx

    import sys try: file_in = open("in.txt", 'r') file_out = open("out.txt", "w+") except IOError: print('无法打开', file_in) else: i = 1 for line in file_in: print(line.rstrip( )) file_out.write("line...

    java通过js上传文件

    简单的利用java与js实现文件...// out.print("保存在服务器上 的地址为:"+path+"&lt;/font&gt;"); } } }catch(Exception e){ e.printStackTrace(); } } response.sendRedirect("smartupload.jsp"); } }

    java io InputStream and outputStream

    System.out.print((char) cont[i]); } is.close();// 关闭文件 // 保存文件 File file2 = new File("bbb.txt");// 定位文件 OutputStream os = new FileOutputStream(file2);// 打开文件 ...

    java程序是怎么操作数据库的,可以以常用据库为例,求详细解答,最好能举例。

    &gt;JDBC&lt;-&gt;ODBC桥-&gt;ODBC-&gt;数据库客户机驱动库-&gt;数据库服务器-&gt;返回查询结果,在这种访问中值的我们注意的是虽然JAVA是"Write Once ,Run Anywhere",但是如果通过这种访问的话,需要客户端必须设置ODBC和有相应的数据库...

    截取一个文件的一段内容

    System.out.print(total); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String []args) { ...

    commons-fileupload-1.2.jar和commons-io-1.3.2.jar

    out.print(name+"&nbsp;&nbsp;"+size+" "); } catch (Exception e) { out.println(e); } } else { throw new IOException("fail to upload"); } } } } catch (IOException e) { out.println(e); }...

    Apache Commons fileUpload实现文件上传

    out.print("the upload file name is" + item.getName()); out.print(" "); } } } else { out.print("the enctype must be multipart/form-data"); } %&gt; &lt;html&gt; &lt;head&gt; &lt;meta ...

    IO输入输出留

    System.out.println("是否可写:"+f.canWrite()); System.out.println("是否是隐藏:"+f.isHidden()); System.out.println("文件的绝对路径:"+f.getAbsolutePath()); System.out.println("获得文件名:"+f....

    Spire.Barcode for Java - Java 一维和二维条码快速生成和识别组件

    Spire.Barcode for JAVA 是一款Java条形码组件,开发人员可以使用它在Java应用程序中简洁快速地生成和识别多种一维和二维条码。下载以后解压缩,然后从lib文件夹下导入Spire.Barcode.jar包到你的Java应用程序中即可...

    matlabmatrix.rar_4 3 2 1_Fibonacci

    2) Write a program which accepts an input k from the keyboard, and which prints out the smallest fibonacci number that is at least as large as k. The program should also print out its position in the ...

    Debugging with GDB --2003年6.0

    2 Getting In and Out of gdb . . . . . . . . . . . . . . . . 11 2.1 Invoking gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.1 Choosing files . . . . . . . ....

Global site tag (gtag.js) - Google Analytics