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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值