I'm having trouble with character encoding for a web application. There is a pop up that queryes the database with user input (search a person by name). The problem is that accented characters are being transformed into weird letters like ó => ó
. This is a pretty standard problem but I can't figure out what is going on.
What have I done?
Mainly, follow this.
(request&response).setCharacterEncoding("UTF-8");
web.xml
, server.xml
the character encodign parameter <?xml version='1.0' encoding='utf-8'?>
.text/html; utf-8
on the get posts (which are mainly the ones with problems)But I still have the same problems, although some have been solved, for example, some accented letters sent from the server to the client, are displaying correctly.
I have apache2.2 and tomcat 6 installed.
I don't know what else to do nor what relevant information should I post here (if you need something please tell me)...
Thanks in advance.
Make sure that the encoding is also set right at the database and JDBC driver level. How to do that depends on the DB and JDBC driver make/version. Consult the DB and JDBC driver specific documentation for details. For the MySQL JDBC driver for example, you need to add two specific parameters to the JDBC connection URL.
By the way, 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 <Connector>
in Tomcat's server.xml
.
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 <page-encoding>
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.
In the java option for debian users in
/etc/default/tomcat7
I put this
set JAVA_OPTS=-Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8
then all encodings seem to come across as UTF-8 by default..
For me this resolved the problem.
Finaly helfull for me was this article
My summary:
Add "URIEncoding="UTF-8" attribute to your Connector in server.xml. like,
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
Then add server side filter for character encoding. In the case of tomcat with spring add below lines to web.xml:
<filter>
<filter-name>charsetFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With