如何在Tomcat中设置请求编码? [英] How to set request encoding in Tomcat?

查看:30
本文介绍了如何在Tomcat中设置请求编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Java 网络应用程序有问题.

I have a problem in my Java webapp.

这是 index.jsp 中的代码:

Here is the code in index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<% request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>

        <form action="index.jsp" method="get">
            <input type="text" name="q"/>
        </form>

        Res: <%= request.getParameter("q") %>
    </body>
</html>

当我使用wireshark 发送请求时,我的浏览器会发送此标头:

When I wireshark a request, my browser sends this header:

GET /kjd/index.jsp?q=%C3%A9 HTTP/1.1

...
Accept-Charset: UTF-8,*

Tomcat 服务器返回给我:

And the Tomcat server returns me this:

Content-Type: text/html;charset=UTF-8

但如果我在表单中发送é"(UTF-8 格式的 %C3%A9),则会显示é".

But if I send "é"(%C3%A9 in UTF-8) in my form, "é" is displayed instead.

我的理解是浏览器发送了一个用 UTF-8(%C3%A9)编码的é".

What I understand is that the browser sends an "é" encoded with UTF-8 (the %C3%A9).

但服务器将其解释为 ISO-8859-1.所以%C3被解码为Ã,%A9被解码为©,然后发回UTF-8编码的响应.

But the server interpret this as ISO-8859-1. So the %C3 is decoded as à and %A9 as ©, and then sends back the response encoded in UTF-8.

在代码中,请求应使用 UTF-8 解码:

In the code, the requests should be decoded with UTF-8:

request.setCharacterEncoding("UTF-8");

但是,如果我发送这个网址:

But, if I send this url:

http://localhost:8080/kjd/index.jsp?q=%E9

%E9"用 ISO-8859-1 解码并显示é".

the "%E9" is decocded with ISO-8859-1 and an "é" is displayed.

为什么这不起作用?为什么使用 ISO-8859-1 解码请求?

Why isn't this working? Why requests are decoded with ISO-8859-1?

我已经在 Tomcat 6 和 7 以及 Windows 和 Ubuntu 上试过了.

I've tried it on Tomcat 6 and 7, and on Windows and Ubuntu.

推荐答案

request.setCharacterEncoding("UTF-8"); 只设置请求的编码body(用于 POST 请求),而不是请求 URI 的编码(用于 GET 请求).

The request.setCharacterEncoding("UTF-8"); only sets the encoding of the request body (which is been used by POST requests), not the encoding of the request URI (which is been used by GET requests).

您需要在Tomcat的/conf/的元素中将URIEncoding属性设置为UTF-8server.xml 让 Tomcat 将请求 URI(和查询字符串)解析为 UTF-8.这确实默认为 ISO-8859-1.另请参阅 Tomcat HTTP 连接器文档.

You need to set the URIEncoding attribute to UTF-8 in the <Connector> element of Tomcat's /conf/server.xml to get Tomcat to parse the request URI (and the query string) as UTF-8. This indeed defaults to ISO-8859-1. See also the Tomcat HTTP Connector Documentation.

<Connector ... URIEncoding="UTF-8">

或确保使用与正文相同的编码解析 URI1:

or to ensure that the URI is parsed using the same encoding as the body1:

<Connector ... useBodyEncodingForURI="true">

另见:

  • Unicode - 如何获取字符对吗? - JSP/Servlet 请求
  • 1 来自 Tomcat 的文档(强调我的):

    1 From Tomcat's documentation (emphasis mine):

    存在此设置是为了与 Tomcat 4.1.x 兼容,其中在 contentType 中指定的编码,或使用显式设置Request.setCharacterEncoding 方法也用于参数从网址.默认值为 false.

    This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitly set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is false.

    <小时>

    请去掉 JSP 中的那些 scriptlets.request.setCharacterEncoding("UTF-8"); 在错误的时刻被调用.每当您正确使用 Servlet 来处理请求时,就为时已晚.您更愿意为此使用 过滤器.response.setCharacterEncoding("UTF-8"); 部分已经由 pageEncoding="UTF-8" 在 JSP 顶部隐式完成.


    Please get rid of those scriptlets in your JSP. The request.setCharacterEncoding("UTF-8"); is called at the wrong moment. It would be too late whenever you've properly used a Servlet to process the request. You'd rather like to use a filter for this. The response.setCharacterEncoding("UTF-8"); part is already implicitly done by pageEncoding="UTF-8" in top of JSP.

    我也强烈推荐替换旧的<%= request.getParameter("q") %> scriptlet 来自 EL ${param.q},或使用 JSTL XML 转义 <代码>${fn:escapeXml(param.q)} 防止XSS 攻击.

    I also strongly recommend to replace the old fashioned <%= request.getParameter("q") %> scriptlet by EL ${param.q}, or with JSTL XML escaping ${fn:escapeXml(param.q)} to prevent XSS attacks.

    这篇关于如何在Tomcat中设置请求编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆