카테고리 없음

UTF-8 한글관련 설정하기

상문이 2009. 5. 1. 22:15

* 한글문제 해결위한 준비

DB의 한글이 utf-8 로 설정되어 있는지 보려면,

mysql> show variables like 'c%'
+--------------------------+--------------------------+
| Variable_name | Value |
+--------------------------+--------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:\MySQL\share\charsets/ |
| collation_connection | latin1_swedish_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
| concurrent_insert | ON |
| connect_timeout | 5 |
+--------------------------+--------------------------+

1. 작업문서에서의 설정
웹페이지의 문서속성에서 <head> 에
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
포함시키기
JSP에서는 페이지 상단에 page 지시자를 포함시킨다
<%@ page contentType="text/html;charset=utf-8">

Post 방식의 데이터 전송
request.setCharacterEncoding("utf-8");
Get 방식의 데이터 전송
request.setCharacterEncoding("utf-8");
String data = new String(request.getParameter("data".getBytes("8859_1"),"utf-8");
//Latin-1(8859_1)로 읽어들인 데이터를 다시 utf-8로 재해석하는 코드

xml 파일에
<?xml version="1.0" encoding="utf-8"?>
포함시키기

2. 개발툴에서 인코딩 옵션변경하기 (저장할 때 변경가능)
editplus , eclipse (preferencs - general - workspace - text file encoding

3. 웹페이지의 URL에서 문자열을 읽어올 때 서버가 처리하는 URL의 인코딩 타입을 변경
Latin-1(8859_1)로 세팅된 톰캣 서버에서는 URL을 무조건 영어로 처리하므로 한글 파일은 절대로 읽어들일 수 없다.
파일을 영어로 바꾸어 저장하거나, 한글 파일을 영어로 바꾸는 처리를 해주어야 한다.

4. DB RUL 의 인코딩
jdbc://mysql://localhost:3306/DB명?useUnicode=true&characterEncoding=utf8 [Java 또는 JSP]
jdbc://mysql://localhost:3306/DB명?useUnicode=true&amp;characterEncoding=utf8 [XML]