//經過Content-Type響應頭,通知瀏覽器以何種編碼格式打開內容 response.setHeader("Content-Type", "text/html;charset=UTF-8"); String data = "中國"; OutputStream out = response.getOutputStream(); //先後格式要統一,getBytes()不指定編碼格式時使用平臺默認編碼格式 out.write(data.getBytes("UTF-8"));能夠回寫<meta>標籤來控制瀏覽器解析行爲
String data = "中國"; OutputStream out = response.getOutputStream(); String meta = "<meta http-equiv='content-type' content='text/html;charset=UTF-8' />": out.write(meta.getBtyes()); out.write(data.getBytes("UTF-8"));
數字亂碼: html
//經過Content-Type響應頭,指定響應內容格式及所採用的編碼格式 response.setHeader("Content-Type", "text/html;charset=UTF-8"); String data = "中國"; OutputStream out = response.getOutputStream(); //輸出數字:要先把數字轉爲字符串再getBytes(),不然瀏覽器會將原數字按指定碼錶解析出其它內容,而不會輸出原數字 out.write((3 + "").getBtyes());
//指定響應以UTF-8格式編碼內容 response.setCharacterEncoding("UTF-8"); //通知瀏覽器以何種編碼格式打開內容 response.setHeader("Content-Type", "text/html;charset=UTF-8"); //或response.setContentType("text/html;charset=UTF-8"); String data = "中國"; OutputStream out = response.getWriter(); out.write(data);
//response.setContentType("text/html;charset=UTF-8") //效果等價於setCharacterEncoding("UTF-8")及setHeader("Content-Type","text/html;charset=UTF-8") response.setContentType("text/html;charset=UTF-8"); String data = "中國"; OutputStream out = response.getWriter(); out.write(data);