728x90
반응형
JSP의 charset 설정
- meta 태그의 charset : 이 페이지를 읽는 클라이언트의 웹 브라우저가 해당 페이지를 해석할 때 사용할 html charset
- contentType의 charset : 이 서블릿에서 응답하는 결과물의 타입 및 charset
- pageEncoding의 charset : .jsp를 .java로 변환할 때의 charset
- 현재 이 페이지에서 Eclipse가 사용하고 있는 charset : 우리가 하드 디스크에 저장할 때 사용할 charset, 현재 파일 우클릭 후 properties에서 확인 할 수 있다.
GET 방식으로 한글 보내기
- GET 방식 파라미터 값은 URL에 포함되어 전달되기 때문에 서버에서 해석한다. 서버의 설정을 바꿔줘야 된다.
- server.xml > Connector > URIEncoding="EUC-KR" 추가
<ol>
<li>GET 방식 파라미터 값은 URL에 포함되어 전달되기 때문에 서버에서 해석한다. 서버의 설정을 바꿔줘야 된다.</li>
<li>server.xml의 Connector에 URIEncoding="EUC-KR"을 추가</li>
<li><a href="./encoding_test/servlet?taste=오렌지">서블릿으로 보내기</a></li>
<li><a href="./encoding_test/get.jsp?taste=콜라">.jsp로 보내기</a></li>
</ol>
package chap05.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/encoding_test/servlet")
public class EncodingTestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("한글 잘 나오나? " + request.getParameter("taste"));
response.sendRedirect("../enconding.jsp");
}
}
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>한글이 잘 나오는지 보는 곳</title>
</head>
<body>
<%=request.getParameter("taste") %>
</body>
</html>
POST 방식으로 한글 보내기
- POST 방식 파라미터 값은 요청 객체 내부에 실려서 서버에 도착하므로 URIEncoding으로 해결되지 않는다.
- request.setCharacterEncoding("EUC-KR");
<ol>
<li>POST 방식 파라미터 값은 요청 객체 내부에 실려서 서버에 도착하므로 URIEncoding으로 해결되지 않는다.</li>
<li>request.setCharacterEncoding("EUC-KR")로 요청 객체의 인코딩 방식을 설정하여 해결할 수 있다.</li>
<li><button type="submit" form="form1">서블릿으로 보내기</button></li>
<li><button type="submit" form="form2">.jsp로 보내기</button></li>
</ol>
<form id="form1" action="./encoding_test/servlet" method="POST">
<input type="hidden" name="price" value="천오백원" />
</form>
<form id="form2" action="./encoding_test/post.jsp" method="POST">
<input type="hidden" name="price" value="이만오천원" />
</form>
package chap05.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/encoding_test/servlet")
public class EncodingTestServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("EUC-KR");
System.out.println(request.getParameter("price"));
response.sendRedirect("../encoding.jsp");
}
}
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<% request.setCharacterEncoding("EUC-KR"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>POST data</title>
</head>
<body>
한글 데이터 : <%= request.getParameter("price") %>
</body>
</html>
728x90
반응형
'JAVA > JSP' 카테고리의 다른 글
[JSP] XML (0) | 2023.06.23 |
---|---|
[JSP] Context Path (0) | 2023.06.23 |
[JSP] Session (0) | 2023.06.23 |
[JSP] Forward와 Redirect (0) | 2023.06.19 |
[JSP] Form (0) | 2023.06.19 |