0

Short version:

I'm trying to send a JSON string, and make sure it can handle special characters. In my case, the critical letters are Æ Ø Å æ ø å, which occur in the Danish and Norwegian standard alphabet.

I'm sending the JSON from a jsp page -> through JQuery/ajax $.getJSON -> to a java servlet (and ultimately saving it to database)

right before the $.getJSON is sent, I have confirmed that the json data String is {"value": "Æ Ø Å æ ø å"}. However, when I retrieve the String through java's HttpRequest.getParameter("jsonData") it gives me {"value": "� � � � � �"}

adding the line $.ajaxSetup({ scriptCharset: "utf-16" , contentType: "application/json; charset=utf-16"}); just ahead of the $.getJSON, made it work in google chrome, but not in IE10...

How can I get the client side and the server side to understand each other?



Additional information (Long version of question)

environments: I'm running the java web application on a Weblogic 11g server. My java version is 1.6 (limited by weblogic, and company standards).

previous attempts, and research:

I know this is an encoding issue. I know that java Strings are encoded in UTF-16. I also know that my jsp page has UTF-8 set as it's encoding: <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />

I figure it must be possible to tell the java servlet that the incoming bytes are in a certain encoding? But a lot of resources on the subject simply ask me to to streamline all of my modules (server, database and client side) to use the same encoding. This would probably work, but it would require a lot of work, and I am not even sure I have the permissions required to decide the encoding of all of my modules. And to me, it also makes as much sense as the following Q&A:

Q: How can we get chinese, spanish and russian people to understand each other?
A: Make them switch their mother tongue into a common language

No, there must be a way to add a translator into the mix here, right?

Settings files, and encoding properties I have come over some qestions and answers who seem to claim that I need to change some settings in the server that is hosting my web app. I could do that for my testing server, but I can't for the actual hosting server, as it is not mine personally to play with. Unless I can make specific settings for this particular web application, and not the whole server.

However, I have only come across descriptions on how to do that for apache tomcat servers...

Previous results:

When I first started to try and solve this problem, only the capital letters were an issue. The server side received String was {"value": "� � � æ ø å"} I experimented with a lot of different approaches, and I can not remember what made the lower case letters also misinterpreted, but it does not make too much of a difference. In any case, I need to know exactly how I can control this, without a doubt.

Resources: My favorite article about String encoding so far is this one, by stack exchange CEO, Joel Spolsky. It has given me the knowledge i need to understand why my problem happens, but not how to solve it. I know that there is a difference between UTF-8 and UTF-16 (and Latin1_GENERAL_CI_AS, which my database uses), but what I don't know is how to tell the different layers of my web application to expect the correct encoding of the incoming String.

I know there are a LOT of questions and answers on this subject, and mine is probably overlapping quite a few of them. But I have spent so much time on this, really tried to read source code, articles and q&a's, and I have still not been able to make it work. I am sure that I have a missing link on my knowledge, and that I am close, but I definitely could use some help finding what I am missing, as I am obviously not able to find it myself...

Update: how my data is gathered and sent (the "insertvalue" is a string i use to trigger an action class server side, in a strategy pattern set up. It works perfectly, and shouldn't be the focus here)

var jsonDataObject = new Object();
    jsonDataObject.value = "Æ Ø Å æ ø å";

    var jsonData = JSON.stringify(jsonDataObject);

    $.getJSON("insertvalue", {          
        json : jsonData
    }, function(data) {
        })
      .fail(function() {             
        alert( "Error" );
      })
      .always(function() {
      });
    return false;
    }
Community
  • 1
  • 1
jumps4fun
  • 3,994
  • 10
  • 50
  • 96
  • You did not show how the JSON data is being gathered or how the JSON is being send to the servlet. You clearly have an encoding problem, but you did not show any code for the actual operations involved. And you have a discrepancy between the scriptlet charset (UTF-16) and the JSP/HTML charset (UTF-8). – Remy Lebeau Feb 06 '15 at 04:43
  • Added the code. Thanks for the comment. I have made it work, btw, through trial and error, but I ended up adding encoding translators and filters in about ten different places in my code, and I still do not know exactly which of them are doing the right tricks. You say scriptlet charset. Do you mind writing a short reply to what you mean by that? I can't quite grasp which part you refer to here. It might refer to JSP-scriptlets, but if it does, I have none of those, as they are not considered good practice anymore. – jumps4fun Feb 06 '15 at 09:45
  • Is this acceptable as dupe? http://stackoverflow.com/questions/4392610/why-does-post-not-honor-charset-but-an-ajax-request-does-tomcat-6/ – BalusC Feb 06 '15 at 18:39
  • It is close, and very helpful, but the very last step is TomCat specific... – jumps4fun Feb 10 '15 at 13:32

0 Answers0