天天看點

Web--request解決表單參數的中文亂碼問題(GET方式和POST方式)

在填寫表單資料時,難免會輸入中文,如姓名、公司名稱等。由于HTML設定了浏覽器在傳遞請求參數時,采用的編碼方式是UTF-8,但在解碼時采用的是預設的ISO8859-1,是以會導緻亂碼的出現。

Web--request解決表單參數的中文亂碼問題(GET方式和POST方式)

解決POST方式送出中文亂碼

在HttpServletRequest接口中,提供了一個setCharacterEncoding()方法,該方法用于設定request對象的解碼方式。

jsp頁面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>表單</title>
</head>
<body>
    <form action="/JavaEEDemo/request" method="post">
        使用者名:<input type="text" name="username"><br/>
        密&nbsp;&nbsp;碼:<input type="password" name="password"><br/>
        愛&nbsp;&nbsp;好:
        <input type="checkbox" name="hobby" value="sing">唱歌
        <input type="checkbox" name="hobby" value="dance">跳舞
        <input type="checkbox" name="hobby" value="football">足球
        <input type="submit" value="送出">

    </form>
</body>
</html>
           
Web--request解決表單參數的中文亂碼問題(GET方式和POST方式)

背景:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("使用者名:" + username);
        System.out.println("密碼:" + password);

        //擷取參數名為hobby的值
        String[] hobbys = request.getParameterValues("hobby");
        System.out.println("愛好:");
        for (int i = 0; i < hobbys.length; i++) {
            System.out.println(hobbys[i] + ", ");
        }
    }
           

點選送出,背景輸出:

使用者名:張三
密碼:123456
愛好:
sing, 
dance, 
           

從上面的示例可以看出,控制台輸出的參數資訊沒有出現亂碼。

需要注意的是: 該 方 式 隻 對 P O S T 方 式 有 效 \color{red}{該方式隻對POST方式有效} 該方式隻對POST方式有效,而對GET方式無效。

解決GET方式送出中文亂碼

需要在擷取到參數後,先用ISO8859-1解碼,然後再使用UTF-8編碼。

修改表單為GET送出方式:

<form action="/JavaEEDemo/request" method="get">
        使用者名:<input type="text" name="username"><br/>
        密&nbsp;&nbsp;碼:<input type="password" name="password"><br/>
        愛&nbsp;&nbsp;好:
        <input type="checkbox" name="hobby" value="sing">唱歌
        <input type="checkbox" name="hobby" value="dance">跳舞
        <input type="checkbox" name="hobby" value="football">足球
        <input type="submit" value="送出">

    </form>
           

修改背景doGet方法:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        username = new String(username.getBytes("iso8859-1"), "utf-8");
        String password = request.getParameter("password");
        System.out.println("使用者名:" + username);
        System.out.println("密碼:" + password);

        //擷取參數名為hobby的值
        String[] hobbys = request.getParameterValues("hobby");
        System.out.println("愛好:");
        for (int i = 0; i < hobbys.length; i++) {
            System.out.println(hobbys[i] + ", ");
        }
    }
           

通過浏覽器通路表單頁面:

Web--request解決表單參數的中文亂碼問題(GET方式和POST方式)

點選送出,背景輸出:

使用者名:小明
密碼:123456
愛好:
dance, 
football,