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

关于响应编码在字符输出流和字节输出流中的总结

时间:2017-12-10 19:31:51      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:port   text   输出流   meta   文本   package   tco   style   extend   

字符输出流:

package com.it.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletDemo1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//      doGet1(request, response);
//      doGet2(request, response);
        doGet3(request, response);
    }
    
    public void doGet1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 服务器中默认的编码为ISO-8859-1(latin1也是MySQL的默认编码),它不支持中文,tomcat规定的
        
        // 方法1
        // 第一步,告诉服务器应用对响应内容使用UTF-8进行(字符编码)解析文本
        response.setCharacterEncoding("UTF-8");
        
        // 第二步,在响应消息头信息中,告诉客户端要使用什么字符集进行解码
        response.setHeader("content-type", "text/html;charset=UTF-8");
        // 该行相当于
        // out.write("<html><head><title></title><meta charset=‘UTF-8‘></head><body>你好123!</body></html>");
        
        PrintWriter out = response.getWriter();// 得到一个字符输出流
        out.write("你好!doGet1");// 向客户端浏览器响应文本内容
    }
    
    public void doGet2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 服务器中默认的编码为ISO-8859-1(latin1也是MySQL的默认编码),它不支持中文,tomcat规定的

        // 方法2
        // 直接一句话,告诉服务器应用使用UTF-8解析文本,告诉客户端要使用什么编码
        response.setContentType("text/html; charset=UTF-8");
        
        PrintWriter out = response.getWriter();// 得到一个字符输出流
        out.write("你好!doGet2");// 向客户端浏览器响应文本内容
    }
    
    public void doGet3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 服务器中默认的编码为ISO-8859-1(latin1也是MySQL的默认编码),它不支持中文,tomcat规定的
        
        response.setCharacterEncoding("UTF-8");// 不能缺少该行,否则乱码
        PrintWriter out = response.getWriter();// 得到一个字符输出流
        
        // 方法3
        out.write("<html><head><title></title><meta charset=‘UTF-8‘></head><body>你好!doGet3</body></html>");// 向客户端浏览器响应文本内容
        // 查看源文件内容就是:<html><head><title></title><meta charset=‘UTF-8‘></head><body>你好123!</body></html>
        // out是作为局部变量不需要close
    }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

字节输出流:

package com.it.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletDemo2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//      doGet1(request, response);// gbk
        doGet2(request, response);// utf-8
    }

    public void doGet1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletOutputStream sos = response.getOutputStream();// 字节输出流
        // void java.io.OutputStream.write(byte[] b) throws IOException
        sos.write("你好123doGet1".getBytes());// getBytes默认编码为本地平台编码(即:gbk)
    }

    public void doGet2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 必须告知浏览器使用什么字符集进行的编码,才能解码输出的字节流内容
        response.setContentType("text/html;charset=UTF-8");

        ServletOutputStream sos = response.getOutputStream();// 字节输出流
        // void java.io.OutputStream.write(byte[] b) throws IOException
        sos.write("你好123doGet2".getBytes("UTF-8"));// getBytes默认编码为本地平台编码(即:gbk)
    }

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

}

 

关于响应编码在字符输出流和字节输出流中的总结

标签:port   text   输出流   meta   文本   package   tco   style   extend   

原文地址:http://www.cnblogs.com/yeyeyeid/p/servlet-response-encode.html

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