java JSP/GlassFish:如何正确设置 UTF-8 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26770380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSP/GlassFish: how to setup UTF-8 encoding correctly
提问by user46688
I'm looking for help to get all my layers in the stack to UTF-8 encoding.
我正在寻求帮助,将堆栈中的所有层都转换为 UTF-8 编码。
I found this nice article:
我发现了这篇不错的文章:
http://www.javapractices.com/topic/TopicAction.do?Id=206
http://www.javapractices.com/topic/TopicAction.do?Id=206
describing the 3 places I need to worry about encoding. Since my (1) Oracle database is currently set to UTF-8, that leaves the (2) browser and (3) server to worry about.
描述我需要担心编码的 3 个地方。由于我的 (1) Oracle 数据库当前设置为 UTF-8,这让 (2) 浏览器和 (3) 服务器需要担心。
I also found this detailed article
我还找到了这篇详细的文章
http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html#JSPServletRequest
http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html#JSPServletRequest
which I'm trying to follow below, but with some newbie questions about implementation.
我试图在下面遵循,但有一些关于实施的新手问题。
To address the browser, I've made sure to include the following at the top of each JSP page:
为了解决浏览器问题,我确保在每个 JSP 页面的顶部包含以下内容:
<%@page pageEncoding="UTF-8"%>
(For reference, see here).
(作为参考,请参见此处)。
To address the server, I made sure to include the following line in the Java servlet and JSP pages before issuing a request.getParameter()
or request.getAttribute()
statement:
为了解决服务器问题,我确保在发出request.getParameter()
orrequest.getAttribute()
语句之前在 Java servlet 和 JSP 页面中包含以下行:
request.setCharacterEncoding("UTF-8");
Since I'm using GlassFish 3.1.2, I understand it does not use UTF-8 by default, so I need to set it manually somehow.
由于我使用的是 GlassFish 3.1.2,我知道它默认不使用 UTF-8,所以我需要以某种方式手动设置它。
I've seen a lot of websites talking about a file named glassfish-web.xml
. Is this part of the normal glassfish install? I don't know where to find it. I've been using the web.xml
file in my WEB-INF
folder for my web application. Could someone help me figure out whether I need to modify this web.xml
file, or do I need to locate or create a new file named glassfish-web.xml
, to configure encoding for glassfish?
我看到很多网站都在谈论一个名为glassfish-web.xml
. 这是正常 glassfish 安装的一部分吗?我不知道在哪里可以找到它。我一直在将web.xml
文件WEB-INF
夹中的文件用于我的 Web 应用程序。有人能帮我弄清楚我是否需要修改这个web.xml
文件,或者我是否需要找到或创建一个名为glassfish-web.xml
,的新文件来配置 glassfish 的编码?
My web.xml
file starts with:
我的web.xml
文件开头为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
...
For the JSP/servlet request, I include the following line in the web.xml
file
对于 JSP/servlet 请求,我在web.xml
文件中包含以下行
<parameter-encoding default-charset="UTF-8"/>
Is this OK to put in the web.xml
file? Or, does it need to go in some glassfish-web.xml
file?
放入web.xml
文件可以吗?或者,它是否需要进入某个glassfish-web.xml
文件?
For the JSP/servlet response, I put the following in my web.xml
file (see accepted answer here):
对于 JSP/servlet 响应,我将以下内容放入我的web.xml
文件中(请参阅此处接受的答案):
<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> </jsp-property-group> </jsp-config>
<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> </jsp-property-group> </jsp-config>
I'm assuming these lines just insert between <web-app>
and </web-app>
. But, let me know if they should instead go inside some other descriptor (such as <glassfish-web-app>
and </glassfish-web-app>
)?
我假设这些行只是在<web-app>
和之间插入</web-app>
。但是,让我知道他们是否应该进入其他一些描述符(例如<glassfish-web-app>
and </glassfish-web-app>
)?
I also put the following in the JSP <head>
section:
我还在 JSP<head>
部分添加了以下内容:
<meta http-equiv="content-type" content="text/html; charset=utf-8">
Useful references:
有用的参考:
How to get rid of WARNING: PWC4011: Unable to set request character encoding to UTF-8
如何摆脱警告:PWC4011:无法将请求字符编码设置为 UTF-8
https://stackoverflow.com/tags/servlet-filters/info
https://stackoverflow.com/tags/servlet-filters/info
https://wikis.oracle.com/display/GlassFish/FaqHttpRequestParameterEncoding
https://wikis.oracle.com/display/GlassFish/FaqHttpRequestParameterEncoding
回答by unwichtich
The glassfish-web.xml
is a file you can create in your WEB-INF
folder. For Glassfish 3.x it has to look similar to this:
这glassfish-web.xml
是您可以在WEB-INF
文件夹中创建的文件。对于 Glassfish 3.x,它必须类似于以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<jsp-config>
</jsp-config>
<parameter-encoding default-charset="UTF-8" />
</glassfish-web-app>
And you are right, the parameter-encoding setting has to be in this file and not in the web.xml
:)
你是对的,参数编码设置必须在这个文件中,而不是在web.xml
:)
See also:
也可以看看: