JavaWeb-HttpServletResponse(一)
程序员文章站
2022-05-25 15:45:13
一.使用OutputStream流向客户端浏览器输出"中国" 二.使用PrintWriter流向客户端浏览器输出中文数据 三.使用OutputStream或者PrintWriter向客户端浏览器输出数字 ......
一.使用outputstream流向客户端浏览器输出"中国"
1 package gacl.response.study; 2 3 import java.io.ioexception; 4 import java.io.outputstream; 5 import javax.servlet.servletexception; 6 import javax.servlet.http.httpservlet; 7 import javax.servlet.http.httpservletrequest; 8 import javax.servlet.http.httpservletresponse; 9 10 public class responsedemo01 extends httpservlet { 11 12 public void doget(httpservletrequest request, httpservletresponse response) 13 throws servletexception, ioexception { 14 outputchinesebyoutputstream(response);//使用outputstream流输出中文 15 } 16 17 /** 18 * 使用outputstream流输出中文 19 */ 20 public void outputchinesebyoutputstream(httpservletresponse response) throws ioexception{ 21 /**使用outputstream输出中文注意问题: 22 * 在服务器端,数据是以哪个码表输出的,那么就要控制客户端浏览器以相应的码表打开, 23 * 比如:outputstream.write("中国".getbytes("utf-8"));//使用outputstream流向客户端浏览器输出中文,以utf-8的编码进行输出 24 * 此时就要控制客户端浏览器以utf-8的编码打开,否则显示的时候就会出现中文乱码,那么在服务器端如何控制客户端浏览器以以utf-8的编码显示数据呢? 25 * 可以通过设置响应头控制浏览器的行为,例如: 26 * response.setheader("content-type", "text/html;charset=utf-8");//通过设置响应头控制浏览器以utf-8的编码显示数据 27 */ 28 string data = "中国"; 29 outputstream outputstream = response.getoutputstream();//获取outputstream输出流 30 response.setheader("content-type", "text/html;charset=utf-8");//通过设置响应头控制浏览器以utf-8的编码显示数据,如果不加这句话,那么浏览器显示的将是乱码 31 /** 32 * data.getbytes()是一个将字符转换成字节数组的过程,这个过程中一定会去查码表, 33 * 如果是中文的操作系统环境,默认就是查找查gb2312的码表, 34 * 将字符转换成字节数组的过程就是将中文字符转换成gb2312的码表上对应的数字 35 * 比如: "中"在gb2312的码表上对应的数字是98 36 * "国"在gb2312的码表上对应的数字是99 37 */ 38 /** 39 * getbytes()方法如果不带参数,那么就会根据操作系统的语言环境来选择转换码表,如果是中文操作系统,那么就使用gb2312的码表 40 */ 41 byte[] databytearr = data.getbytes("utf-8");//将字符转换成字节数组,指定以utf-8编码进行转换 42 outputstream.write(databytearr);//使用outputstream流向客户端输出字节数组 43 } 44 45 public void dopost(httpservletrequest request, httpservletresponse response) 46 throws servletexception, ioexception { 47 doget(request, response); 48 } 49 50 }
二.使用printwriter流向客户端浏览器输出中文数据
1 package gacl.response.study; 2 3 import java.io.ioexception; 4 import java.io.printwriter; 5 6 import javax.servlet.servletexception; 7 import javax.servlet.http.httpservlet; 8 import javax.servlet.http.httpservletrequest; 9 import javax.servlet.http.httpservletresponse; 10 11 /** 12 * 使用printwriter流向客户端浏览器输出中文数据 13 * 还是输出“中国”这两个zi 14 */ 15 public class responsedemo02 extends httpservlet { 16 17 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 18 //使用printwriter流输出中文 19 outputchinesebyprintwriter(response); 20 21 } 22 /* 23 * 使用printwriter流输出中文 24 */ 25 private void outputchinesebyprintwriter(httpservletresponse response) throws ioexception { 26 string data = "中国"; 27 //设置将字符以“utf-8”编码输出到客户端浏览器 28 response.setcharacterencoding("utf-8"); 29 //获取printwriter输出流 30 printwriter out = response.getwriter(); 31 response.setheader("content-type", "text/html;charset=utf-8"); 32 33 //使用printwriter流向客户端输出字符 34 out.write(data); 35 } 36 37 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 38 doget(request, response); 39 } 40 41 }
三.使用outputstream或者printwriter向客户端浏览器输出数字
1 package gacl.response.study; 2 3 import java.io.ioexception; 4 import java.io.outputstream; 5 import java.io.printwriter; 6 7 import javax.servlet.servletexception; 8 import javax.servlet.servletoutputstream; 9 import javax.servlet.http.httpservlet; 10 import javax.servlet.http.httpservletrequest; 11 import javax.servlet.http.httpservletresponse; 12 13 /** 14 * 使用outputstream或者printwriter向客户端浏览器输出数字 15 */ 16 public class responsedemo03 extends httpservlet { 17 18 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 19 //使用outputstream输出1到客户端浏览器 20 //outputonebyoutputstream(response); 21 //使用printwriter流输出数字1 22 outputonebyprintwriter(response); 23 } 24 //使用printwriter流输出数字1 25 public void outputonebyprintwriter(httpservletresponse response) throws ioexception{ 26 response.setheader("content-type", "text/html;charset=utf-8"); 27 response.setcharacterencoding("utf-8"); 28 printwriter out = response.getwriter();//获取printwriter输出流 29 out.write("使用printwriter流输出数字1:"); 30 out.write(1+""); 31 } 32 //使用outputstream输出1到客户端浏览器 33 private void outputonebyoutputstream(httpservletresponse response) throws ioexception { 34 response.setheader("content-type", "text/html;charset=utf-8"); 35 outputstream outputstream = response.getoutputstream(); 36 outputstream.write("使用outputstream流输出数字1:".getbytes("utf-8")); 37 //outputstream.write(1); 38 outputstream.write((1+"").getbytes()); 39 } 40 41 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { 42 doget(request, response); 43 } 44 45 }