Servlet中文乱码问题

一、解决方法

  1. 请求和响应时都要设置编码,需区分get/post请求,区分字节流和字符流输出。

  2. 对于post请求,需设置request.setCharacterEncoding("utf-8");
    对于get请求,有以下两种方式:
    (1) String sno = new String(request.getParameter("sno").getBytes("ISO8859-1"),"UTF-8");
    (2)设置tomcat编码(测试环境tomcat7),在server.xml对应位置加上URIEncoding="UTF-8"


    1.png
  3. 对于get/post的响应
    若为字节流,需加以下代码:

//让浏览器用utf8来解析返回的数据
response.setHeader("Content-type", "text/html;charset=UTF-8");
OutputStream ps = response.getOutputStream();
String data = sno + name;
//使得放入流的数据是utf8格式
ps.write(data.getBytes("UTF-8"));

若为字符流,需加以下代码

//让浏览器用utf8来解析返回的数据
response.setHeader("Content-type", "text/html;charset=UTF-8");
//设置服务器输出的编码为UTF-8,而不是用默认的ISO8859
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(sno);
out.println(name);

二、例子

public class Student {
    private String sno;
    private String name;

    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "FirstServlet", urlPatterns = "/firstServlet")
public class FirstServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* 对于post请求,该语句为避免中文参数乱码起到关键作用 */
        request.setCharacterEncoding("utf-8");
        String sno = request.getParameter("sno");
        String name = request.getParameter("name");
        System.out.println(sno + name);

        //让浏览器用utf8来解析返回的数据
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        ////设置服务器输出的编码为UTF-8,而不是用默认的ISO8859
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.println(sno);
        out.println(name);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String sno1 = request.getParameter("sno");
        String name1 = request.getParameter("name");
        System.out.println(sno1 + name1);
        //get请求处理中文乱码
        String sno = new String(request.getParameter("sno").getBytes("ISO8859-1"),"UTF-8");
        String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
        System.out.println(sno + name);
        //让浏览器用utf8来解析返回的数据
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        ////设置服务器输出的编码为UTF-8,而不是用默认的ISO8859
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.println(sno);
        out.println(name);
    }

}



    
    登录页面


学号:
姓名:

你可能感兴趣的:(Servlet中文乱码问题)