码迷,mamicode.com
首页 > 其他好文 > 详细

ServletResponse

时间:2015-10-14 14:11:57      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

一、Response的Outputstream输出中文的问题

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //response的outputStream输出数据的问题
        //方法一:程序以什么码表输出,一定要控制浏览器以什么码表打开
        String date="中国";
        OutputStream out=response.getOutputStream();
        //方法二:<meta>标签模拟一个http响应头
        out.write("<meta http-equiv=‘content-type‘ content=‘text/html;charset=UTF-8‘>".getBytes());
        out.write(date.getBytes("UTF-8"));
        
    }

 二、通过response的writer流输出数据的问题

 

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置response类使用的码表,以控制response以什么码表向浏览器写出数据
        response.setCharacterEncoding("UTF-8");
        //指定浏览器以什么码表打开服务器发送的数据
        response.setHeader("content-type", "text/html;charset=UTF-8");
        String date="中国";
        PrintWriter out=response.getWriter();
        out.write(date);
    
    }

 也可以使用一句代码来设置:

response.setContentType("text/html;charset=UTF-8");

 

ServletResponse

标签:

原文地址:http://www.cnblogs.com/lyjs/p/4877161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!