乱码呈现
乱码解释
乱码解决
初步解决
改进版
精华版
在这里介绍了Request请求参数中文乱码问题,以及Response响应,下面我们介绍Response响应的中文乱码问题
乱码呈现
先来一个资源路径,不需要手动关闭流,因为响应结束就会自动关闭
字节输出流ServletOutputStream outputStream = response.getOutputStream()和字符输出流PrintWriter writer = response.getWriter()原理是一样的,这里用字符流举例。
IE浏览器访问
中文出现了乱码
乱码解释
因为Response对象时tomcat创建的,tomcat默认使用的是ISO-8859-1字符集,而IE浏览器默认使用的是GB2312字符集,也就是GBK字符集解码,2者的字符集不一样,所以会出现乱码
乱码解决
初步解决
既然明白了乱码出现的原因,那么我们的解决办法就是在Response写入流对象的时候,就修改其编码字符集。
response.setCharacterEncoding("GBK");
现在我们把Response字符集改为BGK
package com.lingaolu.response;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author 林高禄
* @create 2020-07-10-22:41
*/
@WebServlet("/responseDemo5")
public class ResponseDemo5 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("GBK");
PrintWriter writer = response.getWriter();
writer.println("Java");
writer.println("林大帅");
}
}
浏览器访问
改进版
现在我们有一款浏览器的默认解码字符集为UTF-8,那么好的,我们就把Response字符集改为UTF-8,但是这时候,IE浏览器访问是不是乱码了,所以呢我们还得该告诉浏览器使用什么字符集编码。
response.setHeader("Content-Type","text/html;charset=utf-8");
package com.lingaolu.response;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author 林高禄
* @create 2020-07-10-22:41
*/
@WebServlet("/responseDemo5")
public class ResponseDemo5 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type","text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("Java");
writer.println("林大帅");
}
}
谷歌浏览器访问
IE浏览器访问
再看IE浏览器的解码字符集,变成了utf-8
既然有了response.setHeader("Content-Type","text/html;charset=utf-8");
那么response.setCharacterEncoding("utf-8")就不需要了
因为response.setHeader("Content-Type","text/html;charset=utf-8")已经把response的字符集改为了utf-8
我们删掉response.setCharacterEncoding("utf-8")
package com.lingaolu.response;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author 林高禄
* @create 2020-07-10-22:41
*/
@WebServlet("/responseDemo5")
public class ResponseDemo5 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Content-Type","text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("Java");
writer.println("林大帅");
}
}
重启访问浏览器
精华版
既然response.setHeader("Content-Type","text/html;charset=utf-8")中,Content-Type是唯一的,那么response就提供了一个更简便的方法
response.setContentType("text/html;charset=utf-8");
我们代码调整下,输出也加多了几个汉子
package com.lingaolu.response;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author 林高禄
* @create 2020-07-10-22:41
*/
@WebServlet("/responseDemo5")
public class ResponseDemo5 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("Java");
writer.println("林大帅真帅");
}
}
重启访问
这就解决了response相应中文乱码问题
另外字节输出流ServletOutputStream outputStream = response.getOutputStream()解决中文乱码问题和字符输出流PrintWriter writer = response.getWriter()是一样的。