JAVAWeb——response输出流

1. response.getOutputStream() 

*输出字节流,类型为:ServletOutputStream

*只有print()方法

2. response.getWriter();

*输出字符流,类型为:PrintWriter 

*有print()

  write()

**print方法就是调用write方法实现的,也就是将object转换成String了而已。还有就是print方法提供了多种数据类型,而write都通过转换,大多数变为字符串输出了

在使用response.getWriter()时需要注意默认字符编码为ISO-8859-1,如果希望设置字符流的字符编码为utf-8,可以使用response.setCharaceterEncoding(“utf-8”)来设置。这样可以保证输出给客户端的字符都是使用UTF-8编码的!但客户端浏览器并不知道响应数据是什么编码的!如果希望通知客户端使用UTF-8来解读响应数据,那么还是使用response.setContentType("text/html;charset=utf-8")方法比较好,因为这个方法不只会调用response.setCharaceterEncoding(“utf-8”),还会设置content-type响应头,客户端浏览器会使用content-type头来解读响应数据。

https://blog.csdn.net/Ada_yangyang/article/details/82191865

猜你喜欢

转载自blog.csdn.net/m0_37934678/article/details/86525304