[Servlet] html-POST/GET 방식 encoding 문제 해결법

GET 방식의 경우, 인코딩을 하는 경우

- URI 자체를 인코딩 함

- Servers 프로젝트/ server.xml 파일에서 수정해야 함.


- Connector 태그의 URIEncoding 속성을 지정해야 함


POST 방식의 경우, 인코딩을 하는 경우
-post 방식으로 한글을 읽은 경우 오류 발생













- 자체적으로 '숨겨져'있기 때문에 Parameter 수집 전, 인코딩 방식을 지정해야 함
- equest.setCharacterEncoding() 함수 사용



※POST, GET 방식의 인코딩 방식은 상이하다.

+)다수의 값(check box 등)을 입력 받는 경우, request의 gerParameter가 아닌 별도의 함수

getParameterValues() 를 사용(String 요소를 갖는 배열이 반환됨)

예제)
<html 스크립트>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<!-- alt +shift+방향키 : 범위 지정 줄 해당 방향으로 복사 -->
 
<body>
    <form action="ex10join" method="post">
        <!-- 표 -->
        <table width = "400px">
            <!-- step 1 -->
            <tr>
                <th bgcolor = "gray" colspan="2" height = "50px" align="left">
                    step1 : 아이디/비번 입력 
                </th>
            </tr>
 
        <tr bgcolor = "whitesmoke" height = "35px">
            <td align="right">아이디</td>
               
            <td> <input type="text" name = "id"></td>
        </tr>
 
        <tr bgcolor = "whitesmoke" height = "35px">
            <td align="right">비밀번호</td>
 
            <td> <input type="password" name = 'password'></td>
        </tr>
 
        <tr bgcolor = "whitesmoke" height = "35px">
            <td align="right">비밀번호 확인</td>
 
            <td> <input type="password" name = 'c_password'></td>
        </tr>
 
        <!-- step2 -->
            <tr>
                <th  bgcolor = "gray" colspan="2" height = "50px" align="left">step2 : 개인정보</th>
            </tr>
 
            <tr bgcolor = "whitesmoke" height = "35px" >
                <td align="right">성별 : </td>
                <td><input type="radio" name = "gender" value="man">
                    여<input type="radio" name = "gender" value="woman">
                </td>
            </tr>
 
            <tr bgcolor = "whitesmoke" height = "35px">
                <td align="right">혈액형 :</td>
                <td><select name="bloodType" id="">
                    <option value="A">A형</option>
                    <option value="B">B형</option>
                    <option value="AB">AB형</option>
                    <option value="O">O형</option>
                </select> </td>
            </tr>
                
 
            <tr bgcolor = "whitesmoke" height = "35px">
                <td align="right">생일 :</td>
                <td><input type="date" name="date"></td>
            </tr>
 
 
            <!-- step3-->
            <tr>
                <th  bgcolor = "gray" colspan="2" height = "50px" align="left">step3 : 선호도</th>
            </tr>
 
            <tr bgcolor = "whitesmoke" height = "35px">
                <td align="right">취미 :</td>
                <td>
                    축구 <input type="checkBox" name="hobby" value = "soccer">
                    야구 <input type="checkBox" name="hobby" value = "baseball">
                    농구 <input type="checkBox" name="hobby" value = "basketball">
                </td>
            </tr>
 
            <tr bgcolor = "whitesmoke" height = "35px">
                <td align="right">좋아하는 색깔 :</td>
                <td><input type="color" name = "colorValue"></td>
 
            </tr>
 
            <!-- step4 -->
            <tr><th  bgcolor = "gray" colspan="2" height = "50px" align="left">step4 : 적고 싶은 말</th></tr>
            <tr  bgcolor = "whitesmoke" height = "35px">
                <td colspan="2"><textarea cols="56" rows="5" name = "talk"></textarea></td>
            </tr>
 
            <tr >
                <td colspan="2" bgcolor = "whitesmoke" align="center">
                   <input type="submit" value="제출하기">
                    <input type="reset" value="다시쓰기">
                    
                </td>
            </tr>
 
        </table>
    </form>
</body>
</html>
cs


※ Java 소스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ex0509;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class ex10join
 */
@WebServlet("/ex10join")
public class ex10join extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        //post 방식 encoding
        request.setCharacterEncoding("UTF-8");
        
        //양식에서 값을 받아옴
        String id = request.getParameter("id");
        String password= request.getParameter("password");
        String c_password= request.getParameter("c_password");
        String gender= request.getParameter("gender");
        String bloodType= request.getParameter("bloodType");
        String date = request.getParameter("date");
        //다수의 값 받아옴
        String []hobby= request.getParameterValues("hobby");
        String colorValue= request.getParameter("colorValue");
        String talk = request.getParameter("talk");
        
        //응답 형식 지정
        response.setContentType("text/html ; charset=utf-8");
        
        PrintWriter out = response.getWriter();
        
        //html 메시지 작성
        out.print("<html>");
        out.print("<head></head>");
        out.print("<body>");
        out.print("id : "+id+"<br>");
        out.print("password : "+password+"<br>");
        out.print("check : "+c_password+"<br>");
        out.print("gender : "+gender+"<br>");
        out.print("bloodtype : "+bloodType+"<br>");
        out.print("hobby : ");
        //for-each 문으로 배열 출력
        for(String a : hobby)
            out.print(a+" ");
        out.print("<br>");
        out.print("data : "+date+"<br>");
        out.print("color : "+colorValue+"<br>");
        out.print("talk : "+talk+"<br>");
        out.print("</body>");
        out.print("</html>");
        }
 
    }
cs


※실행 결과
1)양식 입력























2)입력 후 결과



댓글