Character decoding Error while retriving parameters in Servlet. August 21, 2008
Posted by ninadgawad in Javascript.Tags: AJAX, HTML, Javascript, Servlet
trackback
Problem:
WARNING: Parameters: Character decoding failed. Parameter skipped.
java.io.CharConversionException: isHexDigit
Cause:
- Generally we get this error when we try to add special characters like “%, @, $ .. etc” in the GET/POST request from Ajax Call.
- Since these characters are used to URL specific function like “&” id used to differentiate various URL parameters. This generates a “null” value for the parameter you will try to retrieve in Servlet.
Solution:
The simple way to get over this problem is to massage your Special Character String with “escape()” funtion in javascript.
Example:
Before:
var vUserEnteredData = document.formname.textbox.value ; // textbox = 100%
var parameters = “testId=A101&percentageNeeded=” + vUserEnteredData;
// Some Ajax Code …..
request.setRequestHeader( ‘Content-Type’, ‘application/x-www-form-urlencoded; charset=UTF-8’ );
request.setRequestHeader(“Content-length”, parameters.length);
request.setRequestHeader(“Connection”, “close”);
request.send(parameters);
// Note: Here the “%” will cause java.io.CharConversionException: while retriving the percentageNeeded parameter in Servlet.
After:
var vUserEnteredData = document.formname.textbox.value ; // textbox = 100%
var parameters = “testId=A101&percentageNeeded=” + escape(vUserEnteredData);
// Some Ajax Code …..
request.setRequestHeader( ‘Content-Type’, ‘application/x-www-form-urlencoded; charset=UTF-8’ );
request.setRequestHeader(“Content-length”, parameters.length);
request.setRequestHeader(“Connection”, “close”);
request.send(parameters);
Alternate Solution:
- We can use Ajax Frameworks rather than manually making the Ajax Request like PrototypeJS, DWR etc.
sadasd
Thanks, this helped me!
thanks a lot
thanks this helped me
SIP, thanks u very much
It’s work in part, thank you
Thank you.
Great Job.
This is priceless, thank you very much for posting!
I am using escape(note) before sending the “note” as parameter and URLDecoder.decode(note,”UTF-8″) for decoding the content on java side. However, when there are special characters, I do get following warning,
WARNING: Parameters: Character decoding failed. Parameter skipped.
java.io.CharConversionException: isHexDigit
I am concerned whether it can cause problem in future. Is it safe to do this?
Thanks It helped me But + symbol didnot working…
Its work fine. thanks
Hi, this is really worthful. but only one small thing + symbol didn’t work . Kindly suggest any idea to fix this. Thanks in Advance.
hi folks, really thanks alot…
Thanks a lot 🙂
Very clearly explained…..
Thanks a lot………
Thanks ! 🙂
Yes It works . Great Post
There is an alternate solution to this.
we you mention encoding format in the connector and configure a CharacterEncodingFilter. It will solve the problem also.
sorry typo.
you can mention encoding format in the connector in (server.xml) and configure a CharacterEncodingFilter. It will solve the problem .
put this code in your web.xml and it will take care of your entire appliaction , you have no need to go and change each and every .js or .jsp file to use function.
characterEncodingFilter
org.apache.catalina.filters.SetCharacterEncodingFilter
encoding
UTF-8
force
true
characterEncodingFilter
/*
thank you this was really helpful and allowed me to find my bug!