본문 바로가기
간단히 배우는 JSP 기초

JSP 인코딩 문제 (한글 문제)

by 긴가우딘 2024. 5. 29.

ocar.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
    <% request.setCharacterEncoding("KSC5601"); %>
        <% String carcolor=request.getParameter("carcolor"); String use=request.getParameter("use"); String
            options[]=request.getParameterValues("option"); %>
            <html>

            <head>
                <title>자동차 정보</title>
            </head>

            <body>
                <h2>자동차 정보</h2>
                자동차 색: <% out.println(carcolor); %> <br>
                    사용 형태: <% out.println(use); %> <br><br>
                        옵션:
                        <% if(options !=null) { for(int k=0; k<options.length; k++) { out.println(options[k]); } } %>
            </body>

            </html>

 

옵션이 왜 물음표로 뜨는 것이냐

 

icar.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
    <html>

    <head>
        <title>자동차 정보</title>
    </head>

    <body>
        <h2>자동차 정보</h2>
        <form method="post" action="ocar.jsp">
            자동차 색: <input type="color" name="carcolor"> <br><br>
            사용 형태:
            <input type="radio" name="use" value="buy" checked>구매
            <input type="radio" name="use" value="lease">리스
            <br><br>

            자동차 옵션: <br>
            <input type="checkbox" name="option" value="에어백">에어백 <br>
            <input type="checkbox" name="option" value="네비게이션">네비게이션 <br>
            <input type="checkbox" name="option" value="블랙박스">블랙박스 <br>
            <br><br>
            <input type="reset" value="초기화">
            <input type="submit" value="자동차 등록">
        </form>
    </body>

    </html>

 

ocar.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
    <% request.setCharacterEncoding("UTF-8"); %>
        <% String carcolor=request.getParameter("carcolor"); String use=request.getParameter("use"); String
            options[]=request.getParameterValues("option"); %>
            <html>

            <head>
                <title>자동차 정보</title>
            </head>

            <body>
                <h2>자동차 정보</h2>
                자동차 색: <% out.println(carcolor); %> <br>
                    사용 형태: <% out.println(use); %> <br><br>
                        자동차 옵션:
                        <% if(options !=null) { for(int k=0; k<options.length; k++) { out.println(options[k]); } } %>
            </body>

            </html>

 

인코딩을 UTF-8로 바꾸어 주었더니 해결 완료

 

무슨 문제인가?

  • JSP는 서블릿으로 변환되어 작동 -> https://wlsdbwls614.tistory.com/30
  • 책은 다 POST 방식으로 호출하는 경우
  • 보통 웹 개발에서는 UTF-8로 인코딩
  • form 태그 안에 method=post 넣기
  • 맨 위에 아래 코드 필요
<%@ page contentType="text/html;charset=UTF-8" %>
    <% request.setCharacterEncoding("UTF-8"); %>