Servlet解决中文乱码问题
2012-08-15 15:38
197 查看
首先保证编码一致,jsp用的是GBK。
post 方法提交
servlet 首行 应该用 request.setCharacterEncoding("GBK");
get 方法提交
如变量name 用
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "GBK");
详细讲解:http://blog.csdn.net/xiazdong/article/details/7217022
一、常识了解
1.GBK包含GB2312,即如果通过GB2312编码后可以通过GBK解码,反之可能不成立;
2.java.nio.charset.Charset.defaultCharset() 获得平台默认字符编码;
3.getBytes() 是通过平台默认字符集进行编码;
二、中文乱码出现
在学习任何一门技术时,经常会有初学者遇到中文乱码问题,比如MySQL,是因为在安装时没有设置;而在Servlet中,也会遇到中文乱码问题;
比如:
OutputStream out = response.getOutputStream();
out.write(String );
输出中文时可能会出现乱码;
比如:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
OutputStream out = response.getOutputStream();
String data = "博客";
out.write(data.getBytes("UTF-8"));
}
输出乱码的问题是程序用UTF-8编码,而浏览器用GB2312解码,因此会出现乱码;
Servlet乱码分为request乱码和response乱码;
三、response中文乱码
在网上很有效的解决方法是添加:
response.setCharacterEncoding("UTF-8");
解决不了,后来又搜到一条解决方法是:
respnse.setHeader("content-type","text/html;charset=UTF-8");
两句都填上,后来终于解决了这个问题;
其实我们应该思考一下本质;
post 方法提交
servlet 首行 应该用 request.setCharacterEncoding("GBK");
get 方法提交
如变量name 用
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "GBK");
详细讲解:http://blog.csdn.net/xiazdong/article/details/7217022
一、常识了解
1.GBK包含GB2312,即如果通过GB2312编码后可以通过GBK解码,反之可能不成立;
2.java.nio.charset.Charset.defaultCharset() 获得平台默认字符编码;
3.getBytes() 是通过平台默认字符集进行编码;
二、中文乱码出现
在学习任何一门技术时,经常会有初学者遇到中文乱码问题,比如MySQL,是因为在安装时没有设置;而在Servlet中,也会遇到中文乱码问题;
比如:
OutputStream out = response.getOutputStream();
out.write(String );
输出中文时可能会出现乱码;
比如:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
OutputStream out = response.getOutputStream();
String data = "博客";
out.write(data.getBytes("UTF-8"));
}
输出乱码的问题是程序用UTF-8编码,而浏览器用GB2312解码,因此会出现乱码;
Servlet乱码分为request乱码和response乱码;
三、response中文乱码
在网上很有效的解决方法是添加:
response.setCharacterEncoding("UTF-8");
解决不了,后来又搜到一条解决方法是:
respnse.setHeader("content-type","text/html;charset=UTF-8");
两句都填上,后来终于解决了这个问题;
其实我们应该思考一下本质;
相关文章推荐
- 解决servlet在post/get传递中文乱码的问题
- servlet:解决servlet中文乱码问题
- 解决servlet到jsp中文乱码问题
- jsp和servlet操作mysql中文乱码问题的解决办法
- jsp和servlet操作mysql中文乱码问题的解决办法
- 解决 Servlet 的中文乱码问题的方式
- 完美解决在Servlet中出现一个输出中文乱码的问题
- 解决servlet响应用户中文请求信息的乱码问题
- 解决Servlet中,中文乱码的问题
- 解决servlet中out.print()跳转alert框中文乱码问题
- Servlet传输过程中解决中文乱码问题
- 关于servlet向mysql添加数据时中文乱码问题的解决
- servlet中如何解决中文乱码的问题!...
- jsp+servlet 文件上传 解决中文乱码问题
- 关于servlet中文乱码问题如何解决
- 在Servlet中以下载的方式向浏览器写数据--中文文件乱码问题解决
- Servlet——表单中的中文请求遇到的乱码问题解决
- servlet中文一般乱码问题结解决方式总结
- servlet下载,解决文件名中有中文下载路径出现乱码不能正常下载问题