博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
servlet乱码问题
阅读量:4483 次
发布时间:2019-06-08

本文共 2951 字,大约阅读时间需要 9 分钟。

一.post乱码

原因:

因为post是以二进制流的形式发送到的服务器。服务器收到数据后,默认以iso-8859-1进行编码。

解决:

POST请求乱码解决,只需要在获取请求参数之前调用request.setCharacterEncoding(“UTF-8”); 方法设置字符集 即可。如下:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    // 1.post请求方式的数据是以二进制流的形式发送到服务器。    // 2.那么就说明它缺少一个字符集。所以我们要设置请求体的字符集即可。    // setCharacterEncoding必须要获取请求参数之前调用才有效    request.setCharacterEncoding("UTF-8");    //获取客户端传递过来的用户名参数值    String username = request.getParameter("username");    System.out.println("用户名:" + username);}

 

二.get乱码

原因:

1.页面提交数据时以utf-8对内容进行编码

2.把编码后的内容传到tomcat服务器

3.服务器会用ISO-8859-1解码,导致乱码

解决:

解决乱码的核心思路,就是把得到的乱码按照原来乱码的步骤逆序操作。

1、先以iso-8895-1进行编码
2、然后再以utf-8进行解码

1) 第一种方式 使用URLEncoder 和 URLDecoder 两个类 编解码如:
//获取客户端传递过来的用户名参数值    String username = request.getParameter("username");    System.out.println("用户名:" + username);    // 先对用户名进行解码得到%E7%8E%8B%E6%8C%AF%E5%9B%BD 这样的形式    username = URLEncoder.encode(username, "ISO-8859-1");    // 再进行utf-8编码 一次得到页面上输入的文本内容    username = URLDecoder.decode(username, "UTF-8");    System.out.println("乱码解决后用户名:" + username);2) 第二种方式 使用 String类的方法进行编解码    username = new String(username.getBytes("ISO-8859-1"), "UTF-8");    System.out.println("乱码解决后用户名:" + username);解决乱码的代码如下:public class Params2 extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request,        HttpServletResponse response) throws ServletException, IOException {    //获取客户端传递过来的用户名参数值    String username = request.getParameter("username");    System.out.println("用户名:" + username);    // 先对用户名进行编码得到%E7%8E%8B%E6%8C%AF%E5%9B%BD 这样的形式    //  username = URLEncoder.encode(username, "ISO-8859-1");    //再进行utf-8解码 一次得到页面上输入的文本内容    //  username = URLDecoder.decode(username, "UTF-8");    //      System.out.println("乱码解决后用户名:" + username);    // 先iso-8859-1编码,再utf-8解码           username = new String(username.getBytes("ISO-8859-1"), "UTF-8");    System.out.println("乱码解决后用户名:" + username);    // 获取密码    String password = request.getParameter("password");    System.out.println("密码:" + password);}}
View Code

 

三.返回浏览器中文乱码

原因:

主要是因为服务器输出的字符串的编码和客户端显示字符串的编码不一致。导致乱码问题

解决:

我们只需要设置服务器和客户端的编码相同就可以解决这个问题。

设置服务器的字符串编码    //设置服务器输出的编码为UTF-8    response.setCharacterEncoding("UTF-8");设置客户端的字符串显示编码。    //告诉浏览器输出的内容是html,并且以utf-8的编码来查看这个内容。    response.setContentType("text/html;charset=utf-8");这两行语句要在获取输出流之前执行。才会生效。protected void doGet(HttpServletRequest request,        HttpServletResponse response) throws ServletException, IOException {    //设置服务器输出的编码为UTF-8    response.setCharacterEncoding("UTF-8");    //告诉浏览器输出的内容是html,并且以utf-8的编码来查看这个内容。    response.setContentType("text/html; charset=utf-8");    // 通过response响应对象获取到字符输出流    Writer writer = response.getWriter();    // 往 客户 端 输出数据。    // writer.write("this is response content!");    // 输出中文数据到客户端     writer.write("这是中文的输出");}

 

再次通过浏览器访问。得到的是正确的中文

 

转载于:https://www.cnblogs.com/xuww-blog/p/9389288.html

你可能感兴趣的文章
servlet乱码问题
查看>>
反射+特性实现 类和XML文档的序列化反序列化
查看>>
日常方法
查看>>
用于后台管理基于iview,vue的穿梭框
查看>>
关于HashMap
查看>>
RuntimeException、Exception和error的区别
查看>>
数据库原理知识
查看>>
Git rebase
查看>>
C语言程序结构
查看>>
linux 设置固定IP centOS6.5
查看>>
Java 重写(Override)与重载(Overload) (转子Runood)
查看>>
Linux常用命令总结
查看>>
飞扬的小鸟(NOIP2014)(丧病DP题)
查看>>
UITableView 的使用小点
查看>>
SpringBoot项目在新电脑上的配置运行,包括JDK+MAVEN+Git+SpringBoot配置等
查看>>
bzoj 3754: Tree之最小方差树 模拟退火+随机三分
查看>>
基于JavaMail的Java邮件发送:简单邮件发送
查看>>
那些年,我追过的绘图工具
查看>>
单例实现方式
查看>>
04 Python并发编程(守护进程,进程锁,进程队列)
查看>>