首页 > 代码库 > response的outputStream输出数据的问题

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 = "http://www.mamicode.com/中国";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 = "http://www.mamicode.com/中国";47 48         OutputStream out = response.getOutputStream();49 50         out51                 .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 = "http://www.mamicode.com/中国";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