首页 > 代码库 > 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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。