Get 与Post 请求数据乱码处理 |
您所在的位置:网站首页 › 火狐post请求后出现乱码 › Get 与Post 请求数据乱码处理 |
Get 与Post 请求方式下中文数据的响应处理
1. 前言
某交流群 Just now 提问者: Get 请求,中文乱码怎么处理? 热心人: 对request 设置编码格式为UTF-8 能解决。 就是下面这样 request.setCharacterEncoding(“UTF-8”); response.setCharacterEncoding(“UTF-8”); Soon 提问者: 这样不行,获取数据还是乱码的。 WTF,N 先生心想着,之前就是靠着上面那两行代码行(keng)走(meng)天(guai)下(pian)的。不行吗?我得试试。 2. 模拟Get/Post 请求与响应 2.1 新建一个Servlet 测试编码转换新建一个Java Web Project,在src 下新建package 命名为pers.niaonao.servlet,在此包新建Servlet 命名为GetPostServlet,用作处理前台模拟的请求。配置GetPostServlet 到项目配置文件web.xml。编写index.jsp 用作前端页面模拟发送Get/Post 请求。 2.1.1 web.xml GetPostServlet pers.niaonao.servlet.GetPostServlet GetPostServlet /GetPostServlet index.jsp 2.2 新建一个JSP 模拟请求2.2.1 index.jsp Insert title here 模拟Get 请求 账户: 密码: 模拟Post 请求 账户: 密码:页面请求数据有两个字段name 和password,使name 传递中文数据,password 为英文字母或阿拉伯数字等。 后台响应前台发出的请求,并获取请求携带的数据name、password,做编码转换,使能正常获取中文数据。 下面是Servlet 的处理。 2.2.2 GetPostServlet.java package pers.niaonao.servlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; public class GetPostServlet extends HttpServlet { public final String UTF_8 = "UTF-8"; public final String ISO_8859_1 = "ISO-8859-1"; /** * 字符串编码转换方法 * @param str 字符串 * @param sourceCharset 源编码 * @param targetCharset 目标编码 * @return * @throws UnsupportedEncodingException */ public String changeCharset(String str, String sourceCharset, String targetCharset) throws UnsupportedEncodingException { if (str == null) { return null; } //用旧的字符编码解码字符串。解码可能会出现异常。 byte[] bs = str.getBytes(sourceCharset); //用新的字符编码生成字符串 return new String(bs, targetCharset); } /** * Get 请求响应方法 */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { String name = request.getParameter("name"); String password = request.getParameter("password"); //处理乱码,将通过Get 方式请求数据使用UTF-8 编码解析 name = changeCharset(name, ISO_8859_1, UTF_8); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("$$$ GetServlet 处理后:$$$name: " + name + "password: " + password); //设置response response.setCharacterEncoding(UTF_8); response.setContentType("text/html"); //输出文本到浏览器 response.getWriter().printf(stringBuilder.toString()); } /** * Post 请求响应方法 */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { //处理乱码,将通过Post 方式请求数据使用UTF-8 编码解析 request.setCharacterEncoding(UTF_8); String name = request.getParameter("name"); String password = request.getParameter("password"); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("$$$ PostServlet 处理后:$$$name: " + name + "password: " + password); //设置response response.setCharacterEncoding(UTF_8); response.setContentType("text/html"); //输出文本到浏览器 response.getWriter().printf(stringBuilder.toString()); } }通过Tomcat 部署一下,运行项目,访问index.jsp,在页面输入数据并请求Servlet 来测试。 测试结果如下,可以看到两种请求方式下的中文乱码问题都能被解决。 另外对于Get 请求使用request.setCharacterEncoding(“utf-8”); 确实不能正确处理中文乱码问题。 图2-1、index.jsp 前端页面图
Tomcat 默认的编码为ISO-8859-1,所以Get 请求乱码问题可以通过修改Tomcat 默认配置编码为UTF-8 来处理。但不建议那样处理。 从上面的栗子中可以看到,这里仍然是通过代码处理。此处用了一个字符串编码转换的方法。将请求的数据从编码ISO-8859-1 转换为UTF-8 编码下的数据即可。 /** * 字符串编码转换方法 * @param str 字符串 * @param sourceCharset 源编码 * @param targetCharset 目标编码 * @return * @throws UnsupportedEncodingException */ public String changeCharset(String str, String sourceCharset, String targetCharset) throws UnsupportedEncodingException { if (str == null) { return null; } //用旧的字符编码解码字符串。解码可能会出现异常。 byte[] bs = str.getBytes(sourceCharset); //用新的字符编码生成字符串 return new String(bs, targetCharset); } 3.2 Post 请求方式而Post 则直接对request 对象设置编码格式为UTF-8 即可。相应的响应对象response 也可以设置编码格式为UTF-8。 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); 更多乱码问题的处理方式 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |