如何在servlet中处理UTF-8编码的字符串 [英] How can I handle UTF-8 encoded strings in my servlet

查看:288
本文介绍了如何在servlet中处理UTF-8编码的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在已经连续搜索了四个小时,我不得不放弃,问你们. 我有一个非常简单的表单,它将接受输入并采取行动,然后将其写入我的Oracle DB(支持UTF-8).

After searching for 4 straight hours now, I have to give up and ask you guys. I have a very simple form which will take an input and on action, write it to my Oracle DB (which supports 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>

我要做的就是能够接受é之类的字符并在不更改编码的情况下存储它们的形式.

All I want to do is the form to be able to accept characters such as é or and store them without changing the encoding.

我已经尝试过(并结合使用)诸如设置之类的选项

I already have tried (and combined) options such as setting

request.setCharacterEncoding("UTF-8");

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

or building a new String with encoding parameter:

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

,但它总是以如下形式出现在数据库中:�?��??(输入:收藏)

but it always ends up in the database like this: �?��?? (Input: 收藏)

在此问题上的任何帮助将不胜感激.

Any help on the subject would be greatly appreciated.

推荐答案

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

In Tomcat server.xml, add a URIEncoding="UTF-8" attribute to your Connector like so:

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

文档指出:

这指定字符编码 用于解码URI字节,之后 %xx对URL进行解码.如果不 指定,将使用ISO-8859-1.

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.

可以找到中的一个有用的(有点相关)的​​帖子.这里.

这篇关于如何在servlet中处理UTF-8编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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