5

This is continuation of this question: Java Jersey: Receive form parameter as byte array

I need form data to be posted in UTF-8 even if containing page uses ISO-8859-1 charset. I found a solution for FF, but not for IE. Here is the whole story:

I use Jersey REST web service to receive data posted from simple html <FORM>. When the page uses UTF-8 charset, posted data is also encoded using UTF-8 and everything works fine. But when the page uses ISO-8859-1 charset, FORM data is also posted using ISO-8859-1 and Jersey has trouble with special characters (probably because it expects data to come in UTF-8).

Jersey method looks like this:

@Path("/someMethod")
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String someMethod(@FormParam("someParam") String someParam)
{
    ...
}

Of course if I change the whole page encoding to UTF-8, everything works fine, the problem is that I add form dynamically to existing pages that may use different encoding (in other works I can't tamper with page encoding).

I have solution that works for FF but not for IE. If I add accept-charset="utf-8" attribute to FORM that posts the data, FF correctly encodes all posted data using utf-8 regardless of containing page encoding, but this doesn't work in IE.

Any ideas? Thank you!

Community
  • 1
  • 1
Dima L.
  • 3,443
  • 33
  • 30
  • Couldn't hurt to mention that this is a continuation of this question: http://stackoverflow.com/questions/6282626/java-jersey-receive-form-parameter-as-byte-array – Tyler Jun 09 '11 at 22:43
  • Actually you might want to retag this with tags like html, cross-browser, and IE. The fact that you're using JAX-RS/Jersey doesn't really matter at this point. What matters is your server expects UTF-8 but IE is giving it ISO-8859-1. So at this point it's a browser problem, the way I see it. – Tyler Jun 10 '11 at 01:14
  • I think the answer from http://stackoverflow.com/questions/153527/setting-the-character-encoding-in-form-submit-for-internet-explorer should work for you. – TheCycoONE Oct 06 '11 at 20:03

1 Answers1

3

Ruby on rails applications use this trick: they add in an hidden field an UTF-8 character that cannot be coerced into ISO Latin 1.

The field they use is

<input name="utf8" type="hidden" value="&#x2713;" />

and it ends up in the query parameters as

search?param1=foo&param2=bar&utf8=✓

History note: before utf8=✓, Ruby on rails used a fancy snowman to force UTF-8: http://intertwingly.net/blog/2010/07/29/Rails-and-Snowmen.

gioele
  • 9,748
  • 5
  • 55
  • 80