servlet乱码问题

一.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值