response的outputStream輸出資料的問題

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   資料   

 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         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 = "中國";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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.