public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
test1(resp);
}
//方法1:
public void test1(HttpServletResponse resp) throws IOException, UnsupportedEncodingException {
resp.setHeader("Content-type", "text/html;charset=utf-8");
String data = "中国";
OutputStream output = resp.getOutputStream();
//程序以什么编码输出,那么一定要设置浏览为相对应的编码打开.
output.write(data.getBytes("utf-8"));
}
//方法2:
//模拟meta标签,设置charset为utf-8,这个方法也行. //用html技术中的meta标签模拟了一个http响应头,来控制浏览器的行为
public void test2(HttpServletResponse response) throws Exception{
String data = "中国_第二个";
OutputStream output= response.getOutputStream();
output.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());
output.write(data.getBytes());
}