POST方法乱码:
1:存在乱码的示例:
前端页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" //设置UTF8编码 pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <!-- 头作用,告诉浏览器UTF8编码 --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <base href="<%=basePath%>"> <title>Insert title here</title> </head> <body> <div align="center"> <form action="Encode" method="post"> Name:<input name="name" type="text" /> <br> Sex:<input type="text" name="sex" /><br> <button type="submit">发送</button> </form> </div> </body> </html>
后台Servlet接受参数:
package com.daxin; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Encode */ public class Encode extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //默认编码是ISO8859-1 String name = request.getParameter("name"); String sex = request.getParameter("sex"); System.out.println(name); System.out.println(sex); } }
当我们启动输入中文时候,打印的是:
?¤§é?? ??·
乱码,这个问题的原因就是:Servlet的方法的所有方法默认解码都是ISO-8859-1,而我们前端页面编码是UTF8,所以导致解码乱码!这种方式也存在解决方案:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //默认编码是ISO8859-1 String name = request.getParameter("name"); String sex = request.getParameter("sex"); System.out.println(name); System.out.println(sex); //由于上面是ISO编码错误,所以还原之后再使用UTF8编码 String newName = new String(name.getBytes("ISO-8859-1"),"UTF-8"); String newSex = new String(sex.getBytes("ISO-8859-1"),"UTF-8"); System.out.println(newName); System.out.println(newSex); }
第二种更优解决方案:设置全局的UTF-8编码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //全局设置UTF8编码,该方法只对实体内容中的数据起作用。post的数据是实体内容中的。所以该设置只对post起作用。get方法不起作用 request.setCharacterEncoding("UTF-8"); //默认编码是ISO8859-1 String name = request.getParameter("name"); String sex = request.getParameter("sex"); System.out.println(name); System.out.println(sex); }
GET乱码问题:
由于request.setCharacterEncoding("UTF-8");方法只对实体内容编码起作用,换言之就是只对Post有效,对于GET方法请求依旧无法生效。所以针对Get请求我们只能手动解码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //默认编码是ISO8859-1 String name = request.getParameter("name"); String sex = request.getParameter("sex"); //由于上面是ISO编码错误,所以还原之后再使用UTF8编码 String newName = new String(name.getBytes("ISO-8859-1"),"UTF-8"); String newSex = new String(sex.getBytes("ISO-8859-1"),"UTF-8"); System.out.println(newName); System.out.println(newSex); }
但是通常我们一般都只是实现一个Post方法逻辑,而是将GET请求逻辑转给Post处理,所以此时比较健全的写法是:
package com.daxin; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Encode */ public class Encode extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 全局设置UTF8编码,该方法只对实体内容中的数据起作用。post的数据是实体内容中的 request.setCharacterEncoding("UTF-8"); // 默认编码是ISO8859-1 String name = request.getParameter("name"); String sex = request.getParameter("sex"); if ("GET".equals(request.getMethod())) { name = new String(name.getBytes("ISO-8859-1"), "UTF-8"); } if ("GET".equals(request.getMethod())) { sex = new String(sex.getBytes("ISO-8859-1"), "UTF-8"); } System.out.println(name); System.out.println(sex); } }
结论:乱码问题出现,就是因为编码和解码时候使用的不是一个编码,最终导致乱码。所以解决问题的核心思路就是使用同一个编码解码器。前端一般使用的是UTF8编码,后台默认是ISO8859-1,所以我们将后台解码器使用UTF8即可解决编码问题。