request.setCharacterEncoding() 和 response.setContentType()

学习经验,请多指教!

request.setCharacterEncoding("utf-8");
使用注意事项:
1、设置从request中取得的值,指定容器按照指定的编码格式去解码。
2、只对 POST 方式请求有效。
3、执行 setCharacterEncoding() 之前,不能执行任何 request.getParameter() 。

// 也可以这样处理
String name2 = new String(name1.getBytes("iso-8859-1"),"utf-8");


response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
使用注意事项:
1、生成一个 content-type 消息头,告诉浏览器,服务器返回的数据类型和编码格式。
2、 out 在输出时,会使用指定的编码格式进行编码。
3、必须在 getWriter() 执行之前设置。

猜你喜欢

转载自yanlianshou.iteye.com/blog/2264163