JavaWeb-HttpServletResponse

Posted hellopaidaxing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb-HttpServletResponse相关的知识,希望对你有一定的参考价值。

一.使用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 }

 

以上是关于JavaWeb-HttpServletResponse的主要内容,如果未能解决你的问题,请参考以下文章