tomcat的字符编码问题 [英] Character encoding problems with tomcat
问题描述
我遇到了网络应用程序的字符编码问题。有一个弹出窗口用用户输入查询数据库(按名称搜索某人)。问题是重音字符正在转换成奇怪的字母,如ó=> ó
。这是一个很标准的问题,但我不知道发生了什么。
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.
我做了什么?
Mainly, follow this.
- 在我的tomcat的第一个过滤器设置
(request& ; response.setCharacterEncoding(UTF-8);
- 设置每个
web.xml
server.xml
字符encodign参数<?xml version ='1.0'encoding ='utf-8'?>
。 - 在连接器中将URIEncoding更改为UTF-8。使用firebug,我已经看到内容类型设置为
text / html;
- 将jsp的元类型和@page更改为UTF-8。 li>
- 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.
我安装了apache2.2和tomcat 6.
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)...
提前感谢。
推荐答案
确保编码也设置正确的数据库和JDBC驱动程序级别。如何做到这一点取决于DB和JDBC驱动程序make / version。有关详细信息,请参阅DB和JDBC驱动程序特定文档。例如,对于MySQL JDBC驱动程序,您需要向JDBC连接URL添加两个特定的参数。
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.
顺便说一下,设置XML文件编码和元标记对HTTP请求/响应编码没有影响。对于基于JSP / Servlet的Web应用程序,只应最小化配置以下内容:
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:
-
对于HTTP GET请求,水平。在Tomcat中,这可以通过在Tomcat的
< Connector>
的URIEncoding
> server.xml
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
.
对于HTTP POST请求,使用一个 ServletRequest#setCharacterEncoding ()
For HTTP POST requests, use a filter which does a ServletRequest#setCharacterEncoding()
.
对于JSP生成的HTTP响应,请设置 pageEncoding
基于每个JSP的属性<%@ page%>
,或者更好地设置< page-encoding> $
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.
对于HTTP响应,请在 web.xml
由servlet生成(其中不涉及JSP)使用 ServletResponse#setCharacterEncoding()
。
For HTTP responses generated by servlets (wherein no JSP is been involved!) use ServletResponse#setCharacterEncoding()
.
最后但同样重要的是,确保您的源代码文件也保存为UTF-8。具体配置取决于所使用的编辑器。在Eclipse的情况下,您可以通过<>>窗口>属性>常规>工作区>文本文件编码来控制它。
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.
- Unicode - How to get the characters right?
这篇关于tomcat的字符编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!