response的outputStream输出数据的问题

 1 package cn.itcast.response;
 2 
 3 import java.io.IOException;
 4 import java.io.OutputStream;
 5 import java.io.PrintWriter;
 6 import java.io.UnsupportedEncodingException;
 7 
 8 import javax.servlet.ServletException;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 //在servlet中用outputStream输出数据的问题,以及输出中文的问题
14 public class ResponseDemo extends HttpServlet {
15 
16     public void doGet(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18 
19         test4(response);
20 
21     }
22 
23     private void test4(HttpServletResponse response) throws IOException,
24             UnsupportedEncodingException {
25 
26         OutputStream out = response.getOutputStream();
27         out.write((1 + "").getBytes());
28     }
29 
30     private void test3(HttpServletResponse response) throws IOException,
31             UnsupportedEncodingException {
32 
33         // 注意,下面写错了浏览器提示下载
34         response.setHeader("content-type", "text/html,charset=UTF-8");
35 
36         String data = "中国";
37 
38         OutputStream out = response.getOutputStream();
39         out.write(data.getBytes("UTF-8"));
40     }
41 
42     private void test2(HttpServletResponse response) throws IOException,
43             UnsupportedEncodingException {
44 
45         // 用html技术中的meta标签模拟一个http响应头,来控制浏览器的行为
46         String data = "中国";
47 
48         OutputStream out = response.getOutputStream();
49 
50         out
51                 .write("<meta http-equiv='content-type' content='text/html;charset='UTF-8'></meta>"
52                         .getBytes());
53         out.write(data.getBytes("UTF-8"));
54     }
55 
56     private void test1(HttpServletResponse response) throws IOException,
57             UnsupportedEncodingException {
58         response.setHeader("content-type", "text/html,charset=UTF-8");
59 
60         String data = "中国";
61 
62         OutputStream out = response.getOutputStream();
63         out.write(data.getBytes("UTF-8"));
64     }
65 
66     public void doPost(HttpServletRequest request, HttpServletResponse response)
67             throws ServletException, IOException {
68 
69     }
70 
71 }
View Code

 

转载于:https://www.cnblogs.com/aineko/p/3817441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值