목요일, 2월 04, 2010

Apache Tomcat 5.x의 HttpServletRequest 클래스의 getParameter([key string]) 버그


String value = request.getParameter("key");

try {
value = new String(value.getBytes(), "UTF-8");
} catch (java.io.UnSupportedEncodingException uee) {
// 지원하지 않는 문자셋
}

을 했을때, 제대로된 결과가 나와야 하지만, Tomcat의 버그로 인해서 제대로된 값이 나오지 않는다.

이유는
HttpServletRequest의 setCharacterEncoding("UTF-8")이 제대로 동작하지 않기 때문으로
Tomcat은 8859_1을 기본값으로 넣어놨기 때문이다.

즉, value = new String(value.getBytes("8859_1"), "UTF-8"); 과 같이 수정하면
제대로된 값을 받을 수 있다.