servlet乱码问题

一.post乱码html

缘由:java

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

解决:tomcat

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乱码app

缘由:ide

1.页面提交数据时以utf-8对内容进行编码
2.把编码后的内容传到tomcat服务器post

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

解决:编码

解决乱码的核心思路,就是把获得的乱码按照原来乱码的步骤逆序操做。
一、先以iso-8895-1进行编码
二、而后再以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("这是中文的输出");
}

 

 再次经过浏览器访问。获得的是正确的中文
相关文章
相关标签/搜索