보내는 코드
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<form action="RequestWebInfo.jsp" method="post"> <!--POST 방식으로 요청-->
영어 : <input type="text" name="eng" value="Bye" /><br />
한글 : <input type="text" name="han" value="잘 가" /><br />
<input type="submit" value="POST 방식 전송" />
</form>
----------------------------------------------------------------------------------------------------
받는 코드
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head><title>내장 객체 - request</title></head>
<body>
<ul>
<li>전송된 값 1 : <%= request.getParameter("eng") %></li>
<li>전송된 값 2 : <%= request.getParameter("han") %></li>
</ul>
</body>
</html>
----------------------------------------------------------------------------------------------------
결과값
----------------------------------------------------------------------------------------------------
전송된값 2가 한글이 제대로 출력이 안되는것을 알 수 있다.
이유는 디코딩때문인데.
request.getParameter은 기본적으로 ISO-8859-1로 디코딩한다.
하지만 보내는 쪽 charset이 utf-8이기 때문에 utf-8로 보내게 된다.
따라서 받는 쪽에서도 utf-8로 입력된 한글을 받게되는데
request.getParameter은 ISO-8859-1로 디코딩하기때문에 한글이 잘못나오는 것이다.
따라서 한글이 깨지게 나오는 것이다.
따라서 ISO-8859-1->utf8
request.setCharacterEncoding("UTF-8");로 바꿔줘야한다.
예시<body>
<%
request.setCharacterEncoding("utf-8");
String han=request.getParameter("han");
%>
<h2>1. 클라이언트와 서버의 환경정보 읽기</h2>
<ul>
<li>전송된 값 2 : <%= han %></li>
</ul>
</body>
----------------------------------------------------------------------------------------------------
결과값
'2022 > JSP' 카테고리의 다른 글
JSP 자바코드 주석 (0) | 2022.01.12 |
---|---|
jsp getParameter값 맞는지 확인하기 (0) | 2022.01.05 |
JSP contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> (0) | 2022.01.05 |
JSP 보낸 파라미터 확인하기 (0) | 2022.01.05 |
JSP URL,URI, 프로토콜, 서버명, 서버포트, 클라이언트 IP주소, 쿼리 스트링 불러오기 (0) | 2022.01.05 |