void |
addHeader(java.lang.String name, java.lang.String value) Adds a response header with the given name and value. |
response.addHeader("Content-type",'text/html;charset=UTF-8");然后就可以手动设置浏览器解析为UTF-8格式
然后调用response.write("汉字".getBytes("UTF-8"));可以设置向responds写入是采用UTF-8格式的。
还有一种方式,直接调用write方法,无论是向responds写入,或是浏览器的解析都是gb2312格式,也不会产生乱码。
设置浏览器的字符集还有种方式:先发送一个http头
OutputStream out=response.getOutputStream();
out.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());
然后再写入数据,就可以使浏览器的解析字符集为UTF-8
另一种思路:直接用字符流输出。
res.setContentType("text/html;charset=UTF-8");这一步相当于两步,第一步设置浏览器解析字符集为UTF-8,设置向