Servlet响应的中文字符集问题

12 篇文章 0 订阅

在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,无乱码

 

字节流以默认编码输出:

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 以字节流用默认编码向客户端输出中文数据,木有乱码
        response.setContentType("text/html");

        String str = "喔呵呵呵呵";
        OutputStream out = response.getOutputStream();
        out.write("</br></br><div align=\"center\" style=\"font-size:25px; color:red\">".getBytes());

        out.write(str.getBytes());

        out.write("</div>".getBytes());
        out.close();
}


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

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 通知客户端查UTF-8码表
        response.setContentType("text/html;charset=utf-8");

        // 或者:
        // response.setHeader("Content-Type","text/html;charset=utf-8");

        String str = "喔哈哈哈哈";
        OutputStream out = response.getOutputStream();
        out.write("</br></br><div align=\"center\" style=\"font-size:25px; color:red\">".getBytes());

        out.write(str.getBytes("utf-8"));

        out.write("</div>".getBytes());
        out.close();
}

字节流输出数字:

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Content-Type", "text/html;charset=utf-8");

        int i = 98;
        OutputStream out = response.getOutputStream();

        out.write("</br></br><div align=\"center\" style=\"font-size:25px; color:red\">"
                .getBytes());

        // out.write(i); 会输出字母b

        // 输出数字98
        out.write((i + "").getBytes());

        out.write("</div>".getBytes());
        out.close();
}

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

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 通知客户端查UTF-8码表
        response.setContentType("text/html;charset=utf-8");
        // 或者:
        // response.setHeader("Content-Type", "text/html;charset=utf-8");

        String str = "喔嘿嘿嘿嘿";
        PrintWriter out = response.getWriter();
        out.write("</br></br><div align=\"center\" style=\"font-size:25px; color:red\">");

        out.write(str);

        out.write("</div>");
        out.flush();
        out.close();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值