标签: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); } }
标签:servlet 乱码
原文地址:http://blog.csdn.net/u014010769/article/details/46532411