0

this is my code

// assume var data has japanese characters
xmlhttp.open("POST","adminUpdate?&value="+data,true); // tried GET as well
xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
xmlhttp.send();

if I insert alert(data) then i can see japanese characters perfectly fine.

But on the server side (servlet class) when I add this code :

String query = request.getParameter("value");
system.out.println(query)

Now I see garbage value ??????

Ok so I added this line server side :

System.out.println("content type : "+ request.getContentType());

and I got this : text/plain;charset=UTF-8

So now my question is if the encoding is set correctly then why I cant see Japanese characters

Harry
  • 1,572
  • 2
  • 17
  • 31
  • machine ??? please explain your question. I can see japanese characters in my web page from database, as well if i hard code japanese characters then also i can see – Harry Jul 03 '14 at 01:11

1 Answers1

1

One option is to send the query parameters as part of the request body and have the content type set to application/x-www-form-urlencoded.

Then, before getting the parameter, set the request's content character encoding

request.setCharacterEncoding("UTF-8");
String query = request.getParameter("value");

Note that wherever you're printing the query value has to be able to display UTF-8 encoded characters.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • and how to "send the query parameters as part of the request body" ?? – Harry Jul 03 '14 at 01:20
  • @HarendraSingh `XmlHttpRequest` has a number of `send` methods. [Check them out here](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest). Use the appropriate one. – Sotirios Delimanolis Jul 03 '14 at 01:21
  • I think thats what am doing, sending the parameters is the request body using xmlhttp.send(); Your link's first command is this only – Harry Jul 03 '14 at 01:25
  • @HarendraSingh What you're currently doing is sending the request parameters as part of the query string. (There's a solution for that, but it's more complicated.) Instead, send the request parameters as form parameters. See the overloaded methods. – Sotirios Delimanolis Jul 03 '14 at 01:26
  • can you suggest an example I tried many but am got getting anywhere – Harry Jul 03 '14 at 01:47
  • 1
    @HarendraSingh [Here you go.](http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest) – Sotirios Delimanolis Jul 03 '14 at 01:48
  • I didnt think that it was that easy, feeling dumb. +1 for the example – Harry Jul 03 '14 at 02:41