Character encoding problems with tomcat

2019-02-07 09:10发布

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.

3条回答
Animai°情兽
2楼-- · 2019-02-07 09:45

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-07 09:52

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.

See also:

查看更多
仙女界的扛把子
4楼-- · 2019-02-07 09:53

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>
查看更多
登录 后发表回答