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

黑马day03 字符流乱码解决方法以及原理分析

时间:2015-06-17 13:27:45      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:servlet 乱码

1.字符流的乱码

response.getWriter().write("中国");这行代码,首先会把中国写入到缓冲区,然后使用老外喜欢用的iso-8859-1进行编码。

然后浏览器使用本地编码表进行解码。因此会产生乱码。

package com.itheima;

import java.io.IOException;

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

public class Demo7Servlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().write("中国");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

运行结果:

技术分享

然后解决方式:就是告知服务器使用gbk进行编码:response.setCharacterEncoding("gbk");

package com.itheima;

import java.io.IOException;

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

public class Demo7Servlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//告知服务器使用gbk进行编码
		response.setCharacterEncoding("gbk");
		response.getWriter().write("中国");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

运行结果:

技术分享

2.如果告知服务器使用utf-8进行编码,但是浏览器会使用默认编码gbk,所以为了防止乱码,因此需要告知浏览器使用utf-8进行解码

package com.itheima;

import java.io.IOException;

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

public class Demo7Servlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//告知服务器使用gbk进行编码
		response.setCharacterEncoding("utf-8");
		//告知浏览器使用utf-8进行解码
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		response.getWriter().write("中国");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
运行结果:

技术分享

3.sun公司为了方便使用所以使用response.setContentType("text/html;charset=utf-8");告知浏览器使用utf-8进行解码,但是底层可以预先知道服务器使用什么编码进行编码。

因此这句话相当于即告知服务器使用ut8-8编码,同时也告知浏览器使用utf-8进行解码。这句话代替了这两句话的作用:

              //告知服务器使用gbk进行编码
response.setCharacterEncoding("utf-8");
//告知浏览器使用utf-8进行解码
response.setHeader("Content-Type", "text/html;charset=utf-8");
response.getWriter().write("中国");


package com.itheima;

import java.io.IOException;

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

public class Demo7Servlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//即告知服务器使用ut8-8编码,同时也告知浏览器使用utf-8进行解码。
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("中国");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

运行结果:

技术分享


黑马day03 字符流乱码解决方法以及原理分析

标签:servlet 乱码

原文地址:http://blog.csdn.net/u014010769/article/details/46532411

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