Character decoding Error while retriving parameters in Servlet. August 21, 2008
Posted by ninadgawad in Javascript.Tags: AJAX, HTML, Javascript, Servlet
21 comments
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.
Clone Javascript Objects July 17, 2008
Posted by ninadgawad in Javascript.Tags: Javascript
8 comments
If you are required to create a backup of JavaScript Object use the clone() function rather than a for loop to individually copy the contents of one JavaScript Object into other Object. This will save a lot of time as well if in future the JavaScript Object is modified your are saved from the task of changing your for loop as well.
// Simple Java Script Object
function UserData (user_ID, user_Data_Preference, user_Theme_No){
this.user_ID = user_ID;
this.user_Data_Preference = user_Data_Preference;
this.user_Theme_No = user_Theme_No ;
}
Consider when the Web Page is loaded this object is populated dynamically and we display the user with respective theme and show data in his preferred form. (e.g. Km/Hr or miles/Hr).
Now you can create a duplicate copy of this JavaScript Object by using the clone() function.
Example:
var objUserDataOrg = new UserData(1, data_101, theme_aqua);
// To Create a duplicate copy
var objUserDataDuplicate = objUserDataOrg.clone();
This will be a new object hence changes in the Old Object will not be reflected in the new and vice-versa.
Pros: This is especially useful when you are required to deal with Array of JavaScript Object rather than a single Object.
Cons: All the attributes of the Object are copied into the new object in case you only want some them to be copied this may not work.