解决Servlet响应出现乱码问题,记录解决问题过程

解决办法

//第一种
response.setContentType("text/html;charset=UTF-8");
//第二种
response.setHeader("Content-type","text/html;charset=utf-8");

感兴趣朋友的可以继续往下看。

这里说明一下,使用自定义Servlet继承HttpServlet的时候,重写父类的方法时不要调用HttpServlet中的doGet()或其他处理请求的方法即super.doGet(),请自定义实现。反例:

package servlets;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("===========>test");
		//这么写会出现405错误,你点进去就会发现。父类啥也没做,就返回个405错误
		//说明人家不想让我们这么做
        super.doGet(req,resp);
        //this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    public void destroy() {
        System.out.println("servlet销毁");
    }

    @Override
    public void init() {
        System.out.println("servlet初始化");
    }
}

响应乱码格式

tomcat8.5响应默认编码是ISO-8859-1

你的谷歌浏览器默认编码应该是GBK

你使用ISO-8859-1格式传输时,浏览器使用GBK格式解码是解不出来的(英文除外),所有才会出现乱码

解决办法,浏览器和tomcat编码一致

1.??? 情况

这种乱码就是tomcat默认编码导致的,不信我给你改一下:
tomcat设置响应编码格式:
response.setCharacterEncoding(“gbk”);

使用GBK

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("===========>test");
        System.out.println("response响应默认编码:"+response.getCharacterEncoding());
        //
        response.setCharacterEncoding("gbk");
        //response.setContentType("text/html;charset=gbk");
        //response.setHeader("Content-type","text/html;charset=utf-8");
        System.out.println("response响应默认编码:"+response.getCharacterEncoding());
        PrintWriter out =  response.getWriter();
        out.println("你好,World!!!");
        out.close();
    }

结果

在这里插入图片描述
在这里插入图片描述
浏览器是可以展示的!

2.浣犲ソ 情况

tomcat使用的utf-8,浏览器是GBK,即这种情况,你设置了:

response.setCharacterEncoding("utf-8");

但是浏览器还是GBK,不一致。

解决办法

一句话,连上一句都不需要:

response.setContentType("text/html;charset=utf-8");

或:

response.setHeader("Content-type","text/html;charset=utf-8");

你要问为什么上面这两句就可以解决?

这两句共同点都是设置了Content-type:text/html;charset=utf-8,这应该是没问题的吧。那么我们就需要从tomcat源码找答案了。

中间省略一万步

从github下载tomcat8.5源码,成功运行。并将servlet测试demo部署进去。具体怎么做的请百度哈。就这样:
在这里插入图片描述

答案

org.apache.coyote.Response类是核心

1.response.setCharacterEncoding(“utf-8”);

只调用了Response类中的setCharacterEncoding()方法:
在这里插入图片描述

2.response.setContentType(“text/html;charset=utf-8”);

以下代码位于org.apache.catalina.connector包下的Response类下:
在这里插入图片描述
图片中标记的两个调用方法分别是org.apache.coyote.Response下setContentTypeNoCharset()和setCharacterEncoding()方法。

也就相当于,response.setContentType(“text/html;charset=utf-8”)作用包含,
response.setCharacterEncoding(“utf-8”);

3.response.setHeader(“Content-type”, “text/html;charset=utf-8”)

以下代码位于org.apache.catalina.connector包下的Response类下:
在这里插入图片描述
图中划线的两个方法最终都会走到,org.apache.coyote.Response下的setHeader()方法
在这里插入图片描述
其中checkSpecialHeader()方法包含setContentType()方法:
在这里插入图片描述
而setContentType()是第二种response.setContentType(“text/html;charset=utf-8”);会执行到。
所以这种方法也可以!

总结

3.response.setHeader(“Content-type”, “text/html;charset=utf-8”)
设置了header,content-type,charset
2.response.setContentType(“text/html;charset=utf-8”);
设置了content-type,charset
1.response.setCharacterEncoding(“utf-8”);
仅设置了charset

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值