Request&Response的乱码问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MyBabyChao/article/details/82353779

HttpServletRequest

中文乱码问题

(1)get请求

get请求发送的数据是包含在请求行内的。

出现乱码的原因:

get请求发送的数据通过浏览器以utf-8方式编码后,发送给Tomcat,
Tomcat是以ISO-8859-1的方式来解码成相应数据。所以我们可以先把数据通过
ISO-8859-1来编码,重新还原数据的utf-8编码字节流,然后再通过utf-8去对字节流解码即可。

即浏览器编码(utf-8)->Tomcat解码(解错了)->重新编码(还原)->解码(utf-8)

1.代码转码

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //username以ISO-8859-1的编码规则还原为字节流,把字节流再以utf-8来解码
        username = new String(username.getBytes("ISO-8859-1"), "utf-8");
        System.out.println("username :" + username + "  password :" + password);
    }

2.修改Tomcat下conf/server.xml文件(指定tomcat默认的字符集)

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

加上URIEncoding=”utf-8”,
需要注意的是如果修改了server.xml文件后,再次使用方法1的话,server.xml文件中的URIEncoding=”utf-8”会被删除掉。

(2)post请求

post请求发送的数据是在请求体中且是以二进制流的形式,即我们在访问的URL里是看不到发送到Tomcat的数据。

出现乱码的原因:

Tomcat是以ISO-8859-1的方式来解码请求体中的数据,而请求体的数据是以utf-8编码的。

1.使用和get相同的处理方法

扫描二维码关注公众号,回复: 3068089 查看本文章

2.使用setCharacterEncoding()

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//指定以utf-8解码,要放在读取数据之前
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username :" + username + "  password :" + password);
}

注意:Tomcat对于Get请求并不会考虑使用request.setCharacterEncoding方法设置的编码。

总结

1.解决Get请求的乱码问题

String data;//data为出现乱码的数据
data = new String(data.getBytes("ISO-8859-1"), "utf-8");

2.解决Post请求的乱码问题

(1)读数据前指定编码规则

request.setCharacterEncoding("utf-8");

(2)使用和get相同的处理方法

HttpServletResponse

中文乱码的问题

乱码原因:

默认使用的是Tomcat的ISO-8859-1编码来发送数据给浏览器,浏览器并不是采用此种编码来解释数据

1.以字符流输出时

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response乱码问题
        //1.设置以utf-8的编码方式输出到浏览器(而不是用ISO-8859-1编码)
        response.setCharacterEncoding("utf-8");

        //2.指定浏览器查看数据时候使用的解码方式
        response.setHeader("Content-type", "text/html; charset=UTF-8");
        response.getWriter().write("计算机学院欢迎你");
    }

2.以字节流输出时

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //指定浏览器查看这份数据使用的解码方式
        response.setHeader("Content-type", "text/html; charset=UTF-8"); 
        //指定中文输出使用的编码方式
        response.getOutputStream().write("计算机学院欢迎您...".getBytes("utf-8"));
    }

总结:响应时,保证我们对数据的编码和浏览器对数据的解码方式保持一致即可

通用方法

无论使用哪种流输出,都可以在输出之前使用setContentType()方法。此方法可设置响应的内容类型(Content-Type),同时内容类型中可以指定编码/解码方式。即如果该方法写在getWriter()/getOutputStream()之前,那么响应内容编码方式将仅从内容类型中去设置.

setContentType()的作用:

1.设置响应的内容类型(浏览器会根据内容类型中编码方式看数据)

2.指定响应数据的编码方式(以哪种编码方式把数据编码后返回给浏览器)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8"); 
        response.getOutputStream().write("欢迎您...".getBytes("utf-8"));
    }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8"); 
        //响应的数据会根据上一行设定的charset进行编码
        response.getWriter().write("欢迎你");
    }

猜你喜欢

转载自blog.csdn.net/MyBabyChao/article/details/82353779
今日推荐