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.
- Setting at the first filter on my tomcat
(request&response).setCharacterEncoding("UTF-8");
- Setting every
web.xml
,server.xml
the character encodign parameter<?xml version='1.0' encoding='utf-8'?>
. - Changing URIEncoding to UTF-8 in the connectors. Using firebug, I already see that content-type is set to
text/html; utf-8
on the get posts (which are mainly the ones with problems) - Change meta types and @page on the jsp's to UTF-8.
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.
In the java option for debian users in
I put this
then all encodings seem to come across as UTF-8 by default..
For me this resolved the problem.
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'sserver.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 inweb.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.
See also:
Finaly helfull for me was this article
My summary:
Add "URIEncoding="UTF-8" attribute to your Connector in server.xml. like,
Then add server side filter for character encoding. In the case of tomcat with spring add below lines to web.xml: