使用 Tomcat 对 servlet 表单提交进行 UTF-8 编码 [英] UTF-8 encoding a servlet form submission with Tomcat
问题描述
我正在尝试将包含 unicode 字符的简单表单发布到 servlet 操作.在 Jetty 上,一切正常.在 Tomcat 服务器上,utf-8 字符被破坏.
I'm attempting to post a simple form that includes unicode characters to a servlet action. On Jetty, everything works without a snag. On a Tomcat server, utf-8 characters get mangled.
我得到的最简单的案例:
The simplest case I've got:
表格:
<form action="action" method="post">
<input type="text" name="data" value="It’s fine">`
</form>`
操作:
class MyAction extends ActionSupport {
public void setData(String data) {
// data is already mangled here in Tomcat
}
}
- 我在 server.xml 中的
上有 URIEncoding="UTF-8" - 动作的第一个过滤器调用 request.setCharacterEncoding("UTF-8");
- 包含表单的页面的内容类型为text/html; charset=UTF-8"
- 向表单添加accept-charset"没有任何区别
- I've got URIEncoding="UTF-8" on
<Connector>
in server.xml - The first filter on the action calls request.setCharacterEncoding("UTF-8");
- The content type of the page that contains the form is "text/html; charset=UTF-8"
- Adding "accept-charset" to the form makes no difference
我可以使它工作的唯一两种方法是使用 Jetty 或将其切换为 method="get".两者都使角色顺利通过.
The only two ways I can make it work are to use Jetty or to switch it to method="get". Both of those cause the characters to come through without a problem.
推荐答案
我有 URIEncoding="UTF-8";在 server.xml 中的
这仅与 GET 请求相关.
That's only relevant for GET requests.
动作的第一个过滤器调用 request.setCharacterEncoding("UTF-8");
好的,这应该适用于 POST 请求.你只需要确保如果你没有调用 getParameter()
、getReader()
、getInputStream()
或其他任何会在调用 setCharacterEncoding()
之前触发解析请求正文.
Fine, that should apply on POST requests. You only need to make sure that if you haven't called getParameter()
, getReader()
, getInputStream()
or anything else which would trigger parsing the request body before calling setCharacterEncoding()
.
包含表单的页面的内容类型是"text/html;charset=UTF-8"
你究竟是如何设置的?如果在 <meta>
中完成,那么您需要了解当页面通过 HTTP 和 HTTP Content- 提供服务时,浏览器会忽略类型
响应头存在.普通的网络服务器已经默认设置了它. 内容类型将仅在页面保存到本地磁盘并从那里查看时使用.
How exactly are you setting it? If done in a <meta>
, then you need to understand that this is ignored by the browser when the page is served over HTTP and the HTTP Content-Type
response header is present. The average webserver namely already sets it by default. The <meta>
content type will then only be used when the page is saved to local disk and viewed from there.
要正确设置响应头字符集,请将以下内容添加到 JSP 的顶部:
To set the response header charset properly, add the following to top of your JSP:
<%@page pageEncoding="UTF-8" %>
顺便说一下,这也会告诉服务器以给定的字符集发送响应.
This will by the way also tell the server to send the response in the given charset.
添加接受字符集";对形式没有区别
它只在 MSIE 中有所不同,但即便如此,它还是错误地使用了它.反正整个属性一文不值.算了.
It only makes difference in MSIE, but even then it is using it wrongly. The whole attribute is worthless anyway. Forget it.
这篇关于使用 Tomcat 对 servlet 表单提交进行 UTF-8 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!