Servlet 输出中文乱码的新收获(2)
精选
转载
查看setCharacterEncoding API文档,进一步发现:
Calling
setContentType(java.lang.String)
with the
String
of
text/html
and calling this method with the
String
of
UTF-8
is equivalent with calling
setContentType
with the
String
of
text/html; charset=UTF-8
.
原来只需要用response.setContentType("text/html; charset=utf-8"); 设定就ok,不需要两次调用。进一步
可反复设置,相互覆盖,据此写出如下测试代码
java 代码
-
- response.setContentType("text/html; charset=gbk");
- response.setCharacterEncoding("utf-8");
-
-
-
-
- PrintWriter pw = response.getWriter();
- pw.print("中文");
结论: