Jerry Welliver
Jerry Welliver

Reputation: 377

Handling utf-8 strings in Java

I have an application that pulls names from the active directory for the domain using ajax calls. Some names have Spanish characters (n tilde for example). I used the utf-8 character set to get the characters to correctly show the data on the form. I can successfully pull the names from the ajax call and load them into the form field. The problem I have is that when the form is posted to the server for the database update, the String cast corrupts the extended characters.

Is there a special String function to handle utf-8? What is the proper method to get the correct values posted to the Oracle tables?

I have done quite a bit of Java coding, but this is my first encounter with the extended characters. Any help will be appreciated.

Thanks.

Upvotes: 0

Views: 2390

Answers (2)

cotton.m
cotton.m

Reputation: 455

Where is this "cast" coming into play?

I am not sure what your application is but there a couple of places where you could be mangling the characters. First, assuming this is some sort of Java EE app make sure that you have set the request encoding in the servlet. See the setCharacterEncoding method of HttpServletRequest. You should use "UTF-8" there.

Second, you should make sure that you have the accept-charset="UTF-8" attribute set on the form variable. (Note - in my experience this rarely is a problem if the page is UTF-8 encoded to begin with but better safe than sorry).

Last make sure that you have specified any encoding options if neccessary for the connection to the database. I don't use Oracle so I don't know but often you'll need to specify to use "unicode" or "utf-8" or the like somewhere where you create the connection.

I would try them in order because it's possible (likely) the first itself might fix the problem.

Upvotes: 4

BillRobertson42
BillRobertson42

Reputation: 12883

You want an OutputStreamWriter. When you construct it, specify that you want to use the "UTF-8" charset. Also make sure you specify that you're sending UTF-8 in your http headers.

Upvotes: 1

Related Questions