Saturday, April 4, 2015

Tomcat Encoding

Tomcat Encoding
[Tomcat] Tomcat Encoding Problems
a. Problem
Queryes the database with user input (search/paging by Japanese string using GET method). The problem is that accented characters are being transformed into weird letters like テスト => テスト.
b. Cause
By default, Tomcat uses ISO-8859-1 character encoding when decoding URLs received from a browser. This can cause problems when encoding is UTF-8, and you are using international characters in the names of attachments or pages.
c. Solution
Setting the XML file encoding and the meta tags have no effect on HTTP request/response encoding. Only the following should be minimally configured for a JSP/Servlet based web application:
For HTTP GET requests, configure it at server level. In Tomcat, that’s to be done by setting the URIEncoding attribute of in Tomcat’s conf/server.xml
XHTML
<Connector port="8080" URIEncoding="UTF-8"/>
<!-- If you are using mod_jk -->
<!--
<Connector port="8009" protocol="AJP/1.3" URIEncoding="UTF-8"/>
-->
For HTTP POST requests, use a filter which does a ServletRequest#setCharacterEncoding()
For HTTP responses generated by JSPs, set pageEncoding attribute of <%@page%> on a per-JSP basis, or, better, set entry in web.xml for an application-wide basis
For HTTP responses generated by servlets (wherein no JSP is been involved!) use ServletResponse#setCharacterEncoding()
Last but not least, make sure that your source code files are also saved as UTF-8. The exact configuration depends on the editor used. In case of Eclipse, you can control it via Window > Properties > General > Workspace > Text File Encoding

No comments:

Post a Comment