请求参数中文乱码问题
出现原因
请求参数中文乱码出现的原因:
浏览器中的编码格式为:utf-8,当有中文数据的时候,会将中文数据以utf-8编码为字节,得到的字节将会存储在请求正文发送给服务器,
但是服务器中的解码格式为:iso8859-1,因此请求正文的字节会被解码成一串乱码
不同版本tomcat中请求参数中文乱码问题
tomcat7中:
1.GET请求
有请求参数中文乱码问题
其对应的两种解决方案:
1)先编码(iso8859-1),再解码(utf-8)
2)告诉服务器,以utf-8去解码路径后参数 ( URIEncoding="UTF-8")
-->注意是解析的URL路径后的参数,因为get请求的参数是直接跟在URL路径上的,但是post请求是在请求体中
2.POST请求
有请求参数中文乱码问题
其对应的两种解决方案:
1)先编码(iso8859-1),再解码(utf-8)
2)告诉服务器,以utf-8解码请求正文
tomcat8、9中:
1.GET请求
没有请求参数中文乱码问题
2.POST请求
有请求参数中文乱码问题
为什么tomcat8、9中GET请求没有请求参数中文乱码问题?
因为tomcat8、9库中已经内部设置好了URIEncoding="UTF-8"
其对应的两种解决方案:
1.先编码(iso8859-1),再解码(utf-8)
2.告诉服务器,以utf-8解码请求正文
!!注意:request.setCharacterEncoding("utf-8");必须放在所有获取请求参数之前,否则声明无效
代码实现1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>请求参数中文乱码问题</title>
</head>
<body>
<form action="/JavaWeb/demo1" method="post">
账户:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<button type="submit">提交</button>
</form>
</body>
</html>
package com.tan.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo1")
public class Demo1Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
byte[] bytes1 = username.getBytes("iso8859-1");
byte[] bytes2 = password.getBytes("iso8859-1");
username = new String(bytes1,"utf-8");
password = new String(bytes2,"utf-8");
System.out.println("username = " + username + ", password = "+ password);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
代码实现2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>请求参数中文乱码问题</title>
</head>
<body>
<form action="/JavaWeb/demo2" method="post">
账户:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<button type="submit">提交</button>
</form>
</body>
</html>
package com.tan.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo2")
public class Demo2Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
System.out.println("username = " + username);
String password = request.getParameter("password");
System.out.println("password = " + password);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
|