gpt4 book ai didi

java - 如何在 servlet 中处理 UTF-8 编码的字符串

转载 作者:行者123 更新时间:2023-11-30 05:56:59 25 4
gpt4 key购买 nike

在连续搜索了四个小时之后,我不得不放弃并询问你们。我有一个非常简单的表单,它将接受输入并执行操作,将其写入我的 Oracle DB(支持 UTF-8)。

<form action="test.jsp" method='GET' accept-charset="UTF-8">
<label for='NAME'>Name</label><input type="text" id="NAME" name="NAME"/>
<button type="submit">Submit</button>
</form>

我想要做的就是表单能够接受 é 等字符并存储它们而不更改编码。

我已经尝试过(并组合了)选项,例如设置

request.setCharacterEncoding("UTF-8");

或者使用编码参数构建一个新的字符串:

new String(request.getParameter("NAME").getBytes(), "UTF-8")

但它总是以这样的方式结束在数据库中:�?????(输入: Collection )

任何有关该主题的帮助将不胜感激。

最佳答案

在 Tomcat server.xml 中,向您的 Connector 添加 URIEncoding="UTF-8" 属性,如下所示:

<Connector port="8080" protocol="HTTP/1.1" 
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>

documentation状态:

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

可以找到一个有用的(稍微相关的)SO 帖子 here .

关于java - 如何在 servlet 中处理 UTF-8 编码的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6453262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com