How can I handle UTF-8 encoded strings in my servl

2019-07-24 17:29发布

问题:

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.

回答1:

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"/>

The documentation states:

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.

A useful (slightly related) SO post can be found here.



回答2:

Have you had a look in the debugger how the string looks like before the insert to the database in java? If it is not mixed up there it is the import function to the database. Which wprapper or utility are you using there?