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
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.
Related
I have a edittext field in my app where end user enters their email for logging in.
When I use getText.toString() to get the value of the field the # symbol is converted automatically to %40 and breaks the passed values to the POST request.
I guess I am missing something simple to stop the auto converting.
I see the issue is with;
#Field(encoded = false, value = "username") String username
A post request parameters get encoded based on the content-type header. You should have your content-type set as "text/html" in order to prevent the encoding.
Alternatively, you can use below code to decode the value as well.
String result = java.net.URLDecoder.decode(receivedStringValue, "UTF-8");
As the server actually requires the field to be "email", worked correctly when I changed the Retro Fit as per below;
//changed value = "username" to value = "email"
#Field(encoded = false, value = "email") String username
Is there any possibility to remove invalid characters from XML response before trying to read entity?
Response searchResponse = webTarget.request(MediaType.APPLICATION_XML).get();
/..
SearchOutput searchOutput = searchResponse.readEntity(SearchOutput.class);
This code works fine when XML is correct but fails when response contains invalid Unicode characters.
How to workaround that assuming I can't change these invalid data in a database?
I have a database with some cratian characters in it like Đ , in the database the character is stored correctly, when using a datatable in primefaces it also shows the character in the webpage just fine.
The problem is that when I send it to the out.println() the character Đ in the name is missing.
for (People p : people) {
System.out.println("p.getName());
}
I tried using String name2 = p.getName().getBytes("ISO-8859-2"); but it still not working
I assume you are using UTF-8 as default encoding on the Database and for Primefaces
Have also a look to this:
Display special characters using System.out.println
I am displaying simplified chines character retrieved from database using the below code snippet but it is displaying junk character
String text="×°ÏäʱÇëÅÄÕÕ"; // retrieved from database
String result=new String(text.getBytes("utf-8"),"GB2312");
Actual output is : �掳�盲�卤�毛����
Expected Output is : 装箱时请拍照
please help
A string always should have the correct characters. Only during conversion to a byte stream the encoding comes into play.
So when text is what you got from the DB then you have the problem already in fetching the string from the DB.
When I'm reading the value of a cookie, the part after '#' is being ignored. So, if my cookie value was "abc#xyz", I'm just getting "abc" when I'm retrieving value by
Cookie cookies [] = request.getCookies ();
pwd=cookies[0].getValue();
whereas, in javascript I'm able to easily read it as "abc#xyz" and even in browser cookies, I can see the value of cookie to be "abc#xyz". What could be wrong here?
My first guess would be a problem related to character encoding. Have you tried to urlencode and -decode the cookie value?
EDIT:
You would retrieve the cookie value by using URLDecoder.decode (cookies[0].getValue(), "utf-8").
In order for that to work, the value must of course be encoded in the first place: Use URLEncoder.encode("abc#xyz", "utf-8"), if you're setting the cookie value from Java, or encodeURIComponent("abc#xyz") to set the value from JavaScript. I don't know how the cookie is set, so you might have to figure this one out for whatever platform you're working on.