HTML:表单不发送 UTF-8 格式的输入 [英] HTML : Form does not send UTF-8 format inputs

查看:26
本文介绍了HTML:表单不发送 UTF-8 格式的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经访问了有关 HTML 中 UTF-8 编码的每个问题,但似乎没有任何事情使它像预期的那样工作.

我添加了 meta 标签:没有任何改变.
我在 form 中添加了 accept-charset 属性:没有任何变化.

<小时>

JSP 文件

<%@ page pageEncoding="UTF-8" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE html><头><meta charset="UTF-8"/><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Editer les sous-titres</title><身体><form method="post" action="/Subtitlor/edit" accept-charset="UTF-8"><h3 name="nameOfFile"><c:out value="${ nameOfFile }"/></h3><input type="hidden" name="nameOfFile" id="nameOfFile" value="${ nameOfFile }"/><c:if test="${ !saved }"><input value ="Enregistrer le travail" type="submit" style="position:fixed; top: 10px; right: 10px;"/></c:if><a href="/Subtitlor/" style="position:fixed; top: 50px; right: 10px;">Retour à la page d'accueil</a><c:if test="${saved }"><div style="position:fixed; top: 90px; right: 10px;"><c:out value="Travail enregistré dans la base de donnée"/>

</c:if><表格边框=1"><c:if test="${ !saved }"><头><th style="weight:bold">原行</th><th style="weight:bold">翻译</th><th style="weight:bold">已翻译</th></thead></c:if><c:forEach items="${subtitles }" var="line" varStatus="status"><tr><td style="text-align:right;"><c:out value="${ line }"/></td><td><input type="text" name="line${ status.index }" id="line${ status.index }" size="35"/></td><td style="text-align:right"><c:out value="${lines[status.index].content }"/></td></tr></c:forEach></表单>

<小时>

服务端

for (int i = 0 ; i <2; i++){System.out.println(request.getParameter("line"+i));}

<小时>

输出

Et ton pére et sa soeurIl ne sera jamais parti.

解决方案

我添加了 meta 标签:没有任何改变.

当页面通过 HTTP 而不是 e.g. 提供时,它确实没有任何影响.来自本地磁盘文件系统(即页面的 URL 是 http://... 而不是例如 file://...).在 HTTP 中,将使用 HTTP 响应头中的字符集.您已经将其设置如下:

<%@page pageEncoding="UTF-8"%>

这不仅会使用 UTF-8 写出 HTTP 响应,还会在 Content-Type 响应头中设置 charset 属性.

webbrowser 将使用这个来解释响应并编码任何 HTML 表单参数.


<块引用>

我在 form 中添加了 accept-charset 属性:没有任何改变.

它仅在 Microsoft Internet Explorer 浏览器中有效.即使那样,它也做错了.永远不要使用它.所有真正的网络浏览器都将使用响应的 Content-Type 标头中指定的 charset 属性.只要您指定accept-charset属性,即使MSIE也会以正确的方式执行此操作.如前所述,您已经通过 pageEncoding 正确设置了它.


去掉 meta 标签和 accept-charset 属性.它们没有任何有用的效果,而且它们只会长期混淆自己,甚至在最终用户使用 MSIE 时使情况变得更糟.只需坚持 pageEncoding.除了在所有 JSP 页面上重复 pageEncoding,您还可以在 web.xml 中全局设置它,如下所示:

<jsp-property-group><url-pattern>*.jsp</url-pattern><页面编码>UTF-8</页面编码></jsp-property-group></jsp-config>

如前所述,这将告诉 JSP 引擎使用 UTF-8 编写 HTTP 响应输出并将其设置在 HTTP 响应标头中.在发送回服务器之前,Web 浏览器将使用相同的字符集对 HTTP 请求参数进行编码.

您唯一缺少的步骤是在 getParameterXxx() 调用中返回之前告诉服务器它必须使用 UTF-8 来解码 HTTP 请求参数.如何在全局范围内执行此操作取决于 HTTP 请求方法.鉴于您使用的是 POST 方法,使用以下自动挂钩所有请求的 servlet 过滤器类相对容易实现:

@WebFilter("/*")公共类 CharacterEncodingFilter 实现 Filter {@覆盖public void init(FilterConfig config) 抛出 ServletException {//没有.}@覆盖public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 抛出 IOException, ServletException {request.setCharacterEncoding(UTF-8");chain.doFilter(请求,响应);}@覆盖公共无效销毁(){//没有.}}

仅此而已.在 Servlet 3.0+(Tomcat 7 及更新版本)中,您不需要额外的 web.xml 配置.

您只需要记住,setCharacterEncoding() 方法在之前被调用是非常重要的,第一次使用任何<代码>getParameterXxx() 方法.这是因为它们在第一次访问时只解析一次,然后缓存在服务器内存中.

所以例如以下顺序错误:

String foo = request.getParameter(foo");//错误的编码.//...request.setCharacterEncoding(UTF-8");//尝试设置它.String bar = request.getParameter("bar");//仍然是错误的编码!

在 servlet 过滤器中执行 setCharacterEncoding() 作业将保证它及时运行(至少在任何 servlet 之前).


如果您也想指示服务器使用 UTF-8 解码 GET(而不是 POST)请求参数(您知道在 URL 中 ? 字符之后看到的那些参数,您知道),然后你基本上需要在服务器端配置它.无法通过 servlet API 配置它.例如,如果您使用 Tomcat 作为服务器,则需要在 Tomcat 自己的 元素中添加 URIEncoding="UTF-8" 属性/conf/server.xml.

如果您仍然在 System 的控制台输出中看到 Mojibake.out.println() 调用,那么标准输出本身未配置为使用 UTF-8 的可能性很大.如何做到这一点取决于谁负责解释和呈现标准输出.例如,如果您使用 Eclipse 作为 IDE,则需要设置 Window >首选项 >一般>工作区文本文件编码到UTF-8.

另见:

I've visited each one of the questions about UTF-8 encoding in HTML and nothing seems to be making it work like expected.

I added the meta tag : nothing changed.
I added the accept-charset attribute in form : nothing changed.


JSP File

<%@ page pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Editer les sous-titres</title>
</head>
<body>
    <form method="post" action="/Subtitlor/edit" accept-charset="UTF-8"> 
        <h3 name="nameOfFile"><c:out value="${ nameOfFile }"/></h3> 
        <input type="hidden" name="nameOfFile" id="nameOfFile" value="${ nameOfFile }"/>
        <c:if test="${ !saved }">
            <input value ="Enregistrer le travail" type="submit" style="position:fixed; top: 10px; right: 10px;" />
        </c:if>
        <a href="/Subtitlor/" style="position:fixed; top: 50px; right: 10px;">Retour à la page d'accueil</a>
        <c:if test="${ saved }">
            <div style="position:fixed; top: 90px; right: 10px;">
                <c:out value="Travail enregistré dans la base de donnée"/>
            </div>
        </c:if>
        <table border="1">
            <c:if test="${ !saved }">
                <thead>
                    <th style="weight:bold">Original Line</th>
                    <th style="weight:bold">Translation</th>
                    <th style="weight:bold">Already translated</th>
                </thead>
            </c:if>
            <c:forEach items="${ subtitles }" var="line" varStatus="status">
                <tr>
                    <td style="text-align:right;"><c:out value="${ line }" /></td>
                    <td><input type="text" name="line${ status.index }" id="line${ status.index }" size="35" /></td>
                    <td style="text-align:right"><c:out value="${ lines[status.index].content }"/></td>
                </tr>
            </c:forEach>
        </table>
    </form>
</body>
</html>


Servlet

for (int i = 0 ; i < 2; i++){
    System.out.println(request.getParameter("line"+i));
}


Output

Et ton père et sa soeur
Il ne sera jamais parti.

解决方案

I added the meta tag : nothing changed.

It indeed doesn't have any effect when the page is served over HTTP instead of e.g. from local disk file system (i.e. the page's URL is http://... instead of e.g. file://...). In HTTP, the charset in HTTP response header will be used. You've already set it as below:

<%@page pageEncoding="UTF-8"%>

This will not only write out the HTTP response using UTF-8, but also set the charset attribute in the Content-Type response header.

This one will be used by the webbrowser to interpret the response and encode any HTML form params.


I added the accept-charset attribute in form : nothing changed.

It has only effect in Microsoft Internet Explorer browser. Even then it is doing it wrongly. Never use it. All real webbrowsers will instead use the charset attribute specified in the Content-Type header of the response. Even MSIE will do it the right way as long as you do not specify the accept-charset attribute. As said before, you have already properly set it via pageEncoding.


Get rid of both the meta tag and accept-charset attribute. They do not have any useful effect and they will only confuse yourself in long term and even make things worse when enduser uses MSIE. Just stick to pageEncoding. Instead of repeating the pageEncoding over all JSP pages, you could also set it globally in web.xml as below:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

As said, this will tell the JSP engine to write HTTP response output using UTF-8 and set it in the HTTP response header too. The webbrowser will use the same charset to encode the HTTP request parameters before sending back to server.

Your only missing step is to tell the server that it must use UTF-8 to decode the HTTP request parameters before returning in getParameterXxx() calls. How to do that globally depends on the HTTP request method. Given that you're using POST method, this is relatively easy to achieve with the below servlet filter class which automatically hooks on all requests:

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        // NOOP.
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // NOOP.
    }
}

That's all. In Servlet 3.0+ (Tomcat 7 and newer) you don't need additional web.xml configuration.

You only need to keep in mind that it's very important that setCharacterEncoding() method is called before the POST request parameters are obtained for the first time using any of getParameterXxx() methods. This is because they are parsed only once on first access and then cached in server memory.

So e.g. below sequence is wrong:

String foo = request.getParameter("foo"); // Wrong encoding.
// ...
request.setCharacterEncoding("UTF-8"); // Attempt to set it.
String bar = request.getParameter("bar"); // STILL wrong encoding!

Doing the setCharacterEncoding() job in a servlet filter will guarantee that it runs timely (at least, before any servlet).


In case you'd like to instruct the server to decode GET (not POST) request parameters using UTF-8 too (those parameters you see after ? character in URL, you know), then you'd basically need to configure it in the server end. It's not possible to configure it via servlet API. In case you're using for example Tomcat as server, then it's a matter of adding URIEncoding="UTF-8" attribute in <Connector> element of Tomcat's own /conf/server.xml.

In case you're still seeing Mojibake in the console output of System.out.println() calls, then chances are big that the stdout itself is not configured to use UTF-8. How to do that depends on who's responsible for interpreting and presenting the stdout. In case you're using for example Eclipse as IDE, then it's a matter of setting Window > Preferences > General > Workspace > Text File Encoding to UTF-8.

See also:

这篇关于HTML:表单不发送 UTF-8 格式的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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