2

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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
tannerli
  • 594
  • 9
  • 33

2 Answers2

6

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.

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • That actually did the Trick! Though it's important to set request.setCharacterEncoding("UTF-8"); This looks like the sun info-page was wrong about the option not working for post-forms... – tannerli Jun 23 '11 at 11:58
  • @tannerli, Glad to have been of help. Btw, this encoding encodes parameters only irrespective of the HTTP request. – Buhake Sindi Jun 23 '11 at 12:01
0

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?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Andreas
  • 707
  • 1
  • 6
  • 23
  • Actually, i don't know how to hook up the debugger to my Tomcat server, but i already tried printing it onto stdout in all ways i could think of, and it looks as if it's already "scrambled" as soon as i call request.getParameter() – tannerli Jun 23 '11 at 11:39
  • What is your OS and in which environment this is running – Andreas Jun 23 '11 at 12:03