SW-PRODUCT/개발-웹닭·HTTP

Tomcat charset encoding 교본

굴돌 2012. 11. 30. 16:01



http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8


tomcat에서 UTF-8로 바꾸기 위한 모든 절차를 깔끔하게 정리해놨다.


주의!! 추가로 catalina.sh 같은 시작파일에 -Dfile.encoding=utf-8 같은 옵션을 줘야 한다.

이는 DefaultServlet의 fileEncoding 파라메터와 같으며, static 파일을 읽을 때 사용할 encoding을 정한다.

만약 static 파일을 읽어와서 사용자에게 뿌려줄 생각이면 이 옵션을 켜둬야 함.



What can you recommend to just make everything work? (How to use UTF-8 everywhere).

Using UTF-8 as your character encoding for everything is a safe bet. This should work for pretty much every situation.

In order to completely switch to using UTF-8, you need to make the following changes:

  1. Set URIEncoding="UTF-8" on your <Connector> in server.xml. References: HTTP Connector, AJP Connector.

  2. Use a character encoding filter with the default encoding set to UTF-8

  3. Change all your JSPs to include charset name in their contentType.

    For example, use <%@page contentType="text/html; charset=UTF-8" %> for the usual JSP pages and <jsp:directive.page contentType="text/html; charset=UTF-8" /> for the pages in XML syntax (aka JSP Documents).

  4. Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8.

    Use response.setContentType("text/html; charset=UTF-8") or response.setCharacterEncoding("UTF-8").

  5. Change any content-generation libraries you use (Velocity, Freemarker, etc.) to use UTF-8 and to specify UTF-8 in the content type of the responses that they generate.
  6. Disable any valves or filters that may read request parameters before your character encoding filter or jsp page has a chance to set the encoding to UTF-8. For more information see http://www.mail-archive.com/users@tomcat.apache.org/msg21117.html.



How can I test if my configuration will work correctly?

The following sample JSP should work on a clean Tomcat install for any input. If you set the URIEncoding="UTF-8" on the connector, it will also work with method="GET".

<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
     <title>Character encoding test page</title>
   </head>
   <body>
     <p>Data posted to this form was:
     <%
       request.setCharacterEncoding("UTF-8");
       out.print(request.getParameter("mydata"));
     %>
     </p>
     <form method="POST" action="index.jsp">
       <input type="text" name="mydata">
       <input type="submit" value="Submit" />
       <input type="reset" value="Reset" />
     </form>
   </body>
</html>