Servlet乱码问题解决

对于请求参数的编码处理基本上分为get和post两种情况。


1、POST

index.html

<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>HTML范例</title>
</head>
<body>
    <form method="post" action="hello.do" name="sample">
        姓名:
        <input type="text" name="name">
        <br />
        <button>发送</button>
    </form>
</body>
</html>

如果客户端没有在Content-Type标头中设置字符编码信息,此时使用HttpServletRequest的getCharacterEncoding返回值是null。

在这个情况下,容器若使用默认的编码ISO-8859-1,而客户端使用UTF-8发送非ASCII字符的请求,Servlet直接使用getParameter等方法

取得请求参数值,就会是不正确的结果也就是乱码。

解决办法:

在取得请求参数前添加:

request.setCharacterEncoding("UTF-8");


String name = request.getParameter("name");

 @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>post</title>");
        out.println("</head>");
        out.println("<body>");
        //取得请求参数
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        //输出字符编码信息
        out.println(request.getCharacterEncoding());
        out.println("</body>");
        out.println("<h1>您的姓名是:" + name + "</h1>");
        out.println("</html>");
        out.close();
    }

2、GET

index.html

<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>HTML范例</title>
</head>
<body>
    <form method="get" action="hello.do" name="sample">
        姓名:
        <input type="text" name="name">
        <br />
        <button>发送</button>
    </form>
</body>
</html>

利用request.setCharacterEncoding("UTF-8");来设置Tomcat接收请求的编码格式,只对POST方式提交的数据有效,对GET方式提交的数据无效!

要设置GET的编码,可以修改server.xml文件中,相应的端口的Connector的属性:URIEncoding="UTF-8",这样,GET方式提交的数据才会被正确解码。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"  redirectPort="8443"URIEncoding="UTF-8"/>

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应内容格式
        response.setContentType("text/html;charset=utf-8");
        //取得输出对象
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>get</title>");
        out.println("</head>");
        out.println("<body>");
        //取得请求参数
        //request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        out.println("</body>");
        out.println("<h1>您的姓名是:" + name + "</h1>");
        out.println("</html>");
        out.close();
    }

或者

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
  useBodyEncodingForURI="true"
/>

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应内容格式
        response.setContentType("text/html;charset=utf-8");
        //取得输出对象
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>get</title>");
        out.println("</head>");
        out.println("<body>");
        //取得请求参数
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        out.println("</body>");
        out.println("<h1>您的姓名是:" + name + "</h1>");
        out.println("</html>");
        out.close();
    }


又或者(不用去修改server.xml):

String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");


完整测试用例:

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;

/**
 * Created by N3verL4nd on 2017/1/4.
 */
@WebServlet("/hello.do")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>post</title>");
        out.println("</head>");
        out.println("<body>");
        //取得请求参数
        request.setCharacterEncoding("UTF-8");
        String usr = request.getParameter("usr");
        String psd = request.getParameter("psd");
        //输出字符编码信息
        //out.println(request.getCharacterEncoding());
        out.println("<h1>账号:" + usr + "<br />密码:" + psd + "</h1>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应内容格式
        response.setContentType("text/html;charset=utf-8");
        //取得输出对象
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>get</title>");
        out.println("</head>");
        out.println("<body>");
        //取得请求参数
        request.setCharacterEncoding("UTF-8");
        String usr = request.getParameter("usr");
        String psd = request.getParameter("psd");
        usr = new String(usr.getBytes("ISO-8859-1"), "UTF-8");
        psd = new String(psd.getBytes("ISO-8859-1"), "UTF-8");
        out.println("<h1>账号:" + usr + "<br />密码:" + psd + "</h1>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
}

request.setCharacterEncoding("UTF-8");
String usr = request.getParameter("usr");
String psd = request.getParameter("psd");
usr = new String(usr.getBytes("ISO-8859-1"), "UTF-8");
psd = new String(psd.getBytes("ISO-8859-1"), "UTF-8");
当把

request.setCharacterEncoding("UTF-8");
改为:

request.setCharacterEncoding("ISO-8859-1");
或者删除(Tomcat容器默认编码为ISO-8859-1),则显示正确。

这对于get/post都是一样的。

两种方法的区别:

对于 URL 提交的数据和表单中 GET 方式提交的数据,在接收数据的 JSP 中设置 request.setCharacterEncoding 参数是不行的,因为在 Tomcat5.0 中,默认情况下使用ISO-8859-1 对 URL 提交的数据和表单中 GET 方式提交的数据进行重新编码(解码),而不使用该参数对 URL 提交的数据和表单中 GET 方式提交的数据进行重新编码(解码)。要解决该问题,应该在 Tomcat 的配置文件的 Connector 标签中设置useBodyEncodingForURI 或者 URIEncoding 属性,其中 useBodyEncodingForURI 参数表示是否用 request.setCharacterEncoding 参数对 URL 提交的数据和表单中 GET 方式提交的数据进行重新编码,在默认情况下,该参数为 false (Tomcat4.0 中该参数默认为true );URIEncoding 参数指定对所有 GET 方式请求(包括 URL 提交的数据和表单中 GET 方式提交的数据)进行统一的重新编码(解码)的编码。URIEncoding 和 useBodyEncodingForURI 区别是,URIEncoding 是对所有 GET 方式的请求的数据进行统一的重新编码(解码),而 useBodyEncodingForURI 则是根据响应该请求的页面的request.setCharacterEncoding 参数对数据进行的重新编码(解码),不同的页面可以有不同的重新编码(解码)的编码。所以对于 URL 提交的数据和表单中 GET 方式提交的数据,可以修改 URIEncoding 参数为浏览器编码或者修改 useBodyEncodingForURI 为true ,并且在获得数据的 JSP 页面中 request.setCharacterEncoding参数设置成浏览器编码。



http://www.cnblogs.com/x_wukong/p/3651853.html?utm_source=tuicool

http://zhuhuide2004.iteye.com/blog/562739




Keep it simple!
作者:N3verL4nd
知识共享,欢迎转载。
原文地址:https://www.cnblogs.com/lgh1992314/p/6616254.html