//经过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);