前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Servlet响应的中文字符集问题

Servlet响应的中文字符集问题

作者头像
欠扁的小篮子
发布2018-04-11 11:13:52
1K0
发布2018-04-11 11:13:52
举报
文章被收录于专栏:技术碎碎念技术碎碎念

在Servlet中利用response向客户端浏览器输出中文时有时会遇到乱码问题,总结如下:

response输出流有两种,一是以字节流输出,一是以字符流输出。

 一、以字节流输出:  1.默认编码输出木有乱码  2.通过response的setHeader方法设置编码utf-8,无乱码  3.通过response的setContentType方法设置编码utf-8,无乱码  4.输出数字建议以字符串形式输出

 二、以字符流输出:  1.默认查iso-8859-1码表(SUN的Servlet规范要求的) ,客户端显示乱码  2.通过response的setHeader方法设置编码utf-8,无乱码  3.通过response的setContentType方法设置编码utf-8,无乱码

字节流以默认编码输出:

代码语言:javascript
复制
 1 public void doGet(HttpServletRequest request, HttpServletResponse response)
 2             throws ServletException, IOException {
 3         // 以字节流用默认编码向客户端输出中文数据,木有乱码
 4         response.setContentType("text/html");
 5 
 6         String str = "喔呵呵呵呵";
 7         OutputStream out = response.getOutputStream();
 8         out.write("</br></br><div align=\"center\" style=\"font-size:25px; color:red\">".getBytes());
 9 
10         out.write(str.getBytes());
11 
12         out.write("</div>".getBytes());
13         out.close();
14 }

字节流设置编码为utf-8输出:

代码语言:javascript
复制
 1 public void doGet(HttpServletRequest request, HttpServletResponse response)
 2             throws ServletException, IOException {
 3 
 4         // 通知客户端查UTF-8码表
 5         response.setContentType("text/html;charset=utf-8");
 6 
 7         // 或者:
 8         // response.setHeader("Content-Type","text/html;charset=utf-8");
 9 
10         String str = "喔哈哈哈哈";
11         OutputStream out = response.getOutputStream();
12         out.write("</br></br><div align=\"center\" style=\"font-size:25px; color:red\">".getBytes());
13 
14         out.write(str.getBytes("utf-8"));
15 
16         out.write("</div>".getBytes());
17         out.close();
18 }

字节流输出数字:

代码语言:javascript
复制
 1 public void doGet(HttpServletRequest request, HttpServletResponse response)
 2             throws ServletException, IOException {
 3         response.setHeader("Content-Type", "text/html;charset=utf-8");
 4 
 5         int i = 98;
 6         OutputStream out = response.getOutputStream();
 7 
 8         out.write("</br></br><div align=\"center\" style=\"font-size:25px; color:red\">"
 9                 .getBytes());
10 
11         // out.write(i); 会输出字母b
12 
13         // 输出数字98
14         out.write((i + "").getBytes());
15 
16         out.write("</div>".getBytes());
17         out.close();
18 }

字符流设置编码为utf-8输出:

代码语言:javascript
复制
 1 public void doGet(HttpServletRequest request, HttpServletResponse response)
 2             throws ServletException, IOException {
 3         // 通知客户端查UTF-8码表
 4         response.setContentType("text/html;charset=utf-8");
 5         // 或者:
 6         // response.setHeader("Content-Type", "text/html;charset=utf-8");
 7 
 8         String str = "喔嘿嘿嘿嘿";
 9         PrintWriter out = response.getWriter();
10         out.write("</br></br><div align=\"center\" style=\"font-size:25px; color:red\">");
11 
12         out.write(str);
13 
14         out.write("</div>");
15         out.flush();
16         out.close();
17 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-08-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档