响应中文的解决

本文介绍了在网页响应中处理中文乱码的方法,包括如何正确配置字符编码以确保客户端能够正确解析中文内容,同时对比了使用字符流和字节流输出中文的区别。
    向页面响应的方法:
    * getOutputStream();
    * getWriter();

    *** 这两个方法是互斥的.**
        * 做出响应的时候只能使用其中的一种流响应.

    * 输出中文乱码的处理:
        * 字节流:
            * 设置浏览器默认打开的编码:
                * resposne.setHeader(“Content-Type”,”text/html;charset=UTF-8”);
            * 设置中文字节取出的时候编码.
                * “中文”.getBytes(“UTF-8”);

        * 字符流:
            * 设置浏览器打开的时候的编码
                * resposne.setHeader(“Content-Type”,”text/html;charset=UTF-8”);
            * 设置response的缓冲区的编码
                * response.setCharacterEncoding(“UTF-8”);

            ***** 简化的写法:response.setContentType(“text/html;charset=UTF-8”);


    具体代码:
    /**
     * 向页面做出响应的Servlet
     */


        /**
         * 使用字符流输出中文:
         * * 有没有乱码?为什么?
         *     * 一定会乱码.response使用的字符流有缓冲区的.
         *     * response的字符流的缓冲区的默认的编码是ISO-8859-1.编码根本不支持中文.
         * * 解决:
         *     * 设置response的字符流的缓冲区的编码.
         *     * 设置浏览器默认打开的时候采用的字符集编码.
         * @param response
         * @throws IOException
         */
        private void test4(HttpServletResponse response) throws IOException {
            //设置浏览器默认打开的时候采用的字符编码
            //response.setHeader("Content-Type", "text/html;charset=UTF-8");
            //设置response的字符流的缓冲的编码
            //response.setCharacterEncoding("UTF-8");

            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println("中文啊啊");   
        }

        /**
         * 使用字节流输出中文:
         * * 有没有乱码?为什么?
         *    * 不一定. 
         *    * 解决:
         *       * 设置中文转成字节数组取出的时候的编码
         *       * 设置浏览器默认打开的时候编码
         */
        private void test3(HttpServletResponse response) throws UnsupportedEncodingException, IOException {
            //设置浏览器默认打开的时候采用的字符集编码
            //response.setHeader("Content-Type", "text/html;charset=UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            //设置中文转成字节数组的时候取出的编码
            response.getOutputStream().write("中文".getBytes("UTF-8"));
        }

        /**
         * 使用字符流响应数据
         * @param response
         * @throws IOException 
         */
        private void test2(HttpServletResponse response) throws IOException {
            response.getWriter().println("Hello Writer Response....");
        }

        /**
         * 使用字节流响应数据
         * @param response
         * @throws IOException
         */
        private void test1(HttpServletResponse response) throws IOException {
            response.getOutputStream().write("Hello OutputStream Response....".getBytes());
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值