Servlet接收Get和Post请求时的乱码问题及解决

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

当我们使用get或者post请求向服务端发起请求时,由于客户端没有告诉服务器,请求正文的编码,于是服务器默认用ISO-8859-1进行编码。

1.post请求方式乱码解决。

request.setCharacterEncoding("UTF-8");

2。get请求方式乱码解决。

String name = reuqest.getParameter("name");
拿到原始的二进制数据,用UTF-8进行重新编码
byte b[] = name.getBytes("ISO-8859-1");
String name = new String(b,"UTF-8");

猜你喜欢

转载自blog.csdn.net/qq_36640903/article/details/80764628