標籤: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