In hope this will help u... An approach towards handling the special characters has been mentioned below. Steps to be followed for Special Character Handling Gist of the approach followed [A] Forward Flow - While Adding data - Use escape function to encrypt the field data before submission of the form to the servlet - On getting the values from the HTTP Request object, apply the java.net.URLDecoder.decode method to get the actual content [B] Backward Flow - While Retrieving data - After retrieving data, check if there are spaces and convert the spaces to some escape sequence ('SpAcE' in this case) - On the output of the above step invoke the java.net.URLEncoder.encode method to encrypt the data - On load of the screen, use the unescape function to decrypt the field data and replace the escape sequence for spaces ('SpAcE') with ' '. [A] Forward Flow - While Adding data 1. Just before submitting the form to the servlet, call the escapeFields method, the parameter (singular) to be passed are the field names (only the fields that allow special characters) seperated by comma. E.g. Inside OnClick fn of [Add/Save] button
function fnOnClickOfAdd() { ... ... ... escapeFields('fieldName1,fieldName2,fieldName3,fieldName4,fieldName5'); fnToInvokeServlet(); } 2.and then use decodeString method to decode. [B] Backward Flow - While Retrieving data 1. While extracting the data use encode method 2. OnLoad of the screen call the unescapeFields function to decode the field data JavaScript Source ----------------- // Function Name : escapeFields // Description : All spaces, punctuation, accented characters, and any // other non-ASCII characters are replaced with %xx // encoding, where xx is equivalent to the hexadecimal // number representing the character. // Parameters function escapeFields(msFields) { var maFieldsArray = msFields.split(","); var miIcnt; for (miIcnt=0; miIcnt < maFieldsArray.length; miIcnt++) { field_ = top.getFrameReference('CAFE_form').document.getElementById(maFieldsArray[miI cnt]); if (field_ != null) { field_.value = escape(field_.value); } } } // Function Name : unescapeFields // Description : All characters encoded with the %xx hexadecimal form are // replaced by their ASCII character set equivalents. function unescapeFields(msFields) { var maFieldsArray = msFields.split(","); var miIcnt; for (miIcnt=0; miIcnt < maFieldsArray.length; miIcnt++) { field_ = top.getFrameReference('CAFE_form').document.getElementById(maFieldsArray[miI cnt]); if (field_ != null) { fieldContent_ = field_.value; fieldContent_ = unescape(fieldContent_); field_.value = fieldContent_.replace('SpAcE',' '); } } } Java Source ------------ public class ApplicationUtilityClass { ... ... ... /** * Encodes the embeded spaces into a literal. * */ public static String encodeSpaces(String unformattedString) { String formattedString = null; if (unformattedString != null) { formattedString = ""; for (int i=0; i<unformattedString.length(); i++) { switch (unformattedString.charAt(i)) { case ' ': formattedString += "SpAcE"; break; default: formattedString += unformattedString.charAt(i); } } } return formattedString; } /** * Encodes the String into appropriate ASCII value. * */ public static String encodeString(String normalString) { String methodName = "encodeString"; if (normalString != null) { normalString = encodeSpaces(normalString); String encodedString = java.net.URLEncoder.encode(normalString); return encodedString; } return null; } public static String decodeString(String encodedString) throws com.aig.cafe.framework.util.ActionException { String methodName = "decodeString"; try { if (encodedString != null) { String decodedString = java.net.URLDecoder.decode(encodedString); return decodedString; } } catch (java.lang.Exception e) { throw new com.aig.cafe.framework.util.ActionException(e); } return null; } ... ... ... } > -----Original Message----- > From: Lyubomir Pashov [SMTP:[EMAIL PROTECTED]] > Sent: Tuesday, January 07, 2003 2:36 PM > To: [EMAIL PROTECTED] > Subject: A problem with some characters receiving... > > Hello, > I'm doing a Java chat project. Briefly - the client's part sends and > receives messages (to and from other clients) through the servlet's part > (of the project) (and the Servlet's doPost(...) method is used for this). > I have the following problem: when a user's message (sentence) contains > characters "~" and "&", the other user receives only a part from the > message - restricted to the first occurrence of these characters. > If the massage contains "%" then other user receives nothing. > Also there is problem with the "+". > > So I think I must encode (or encrypt) the message thus so to exclude this > characters from the message, and when the message is received, to decode > (decrypt) it. But, how should I make it? > > Can anybody show me a code snipped to solve this problem? > And... any suggestions are appreciated. > > Thanks, > Lyubomir. > > __________________________________________________________________________ > _ > To unsubscribe, send email to [EMAIL PROTECTED] and include in the > body > of the message "signoff SERVLET-INTEREST". > > Archives: http://archives.java.sun.com/archives/servlet-interest.html > Resources: http://java.sun.com/products/servlet/external-resources.html > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html ___________________________________________________________________________ To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff SERVLET-INTEREST". Archives: http://archives.java.sun.com/archives/servlet-interest.html Resources: http://java.sun.com/products/servlet/external-resources.html LISTSERV Help: http://www.lsoft.com/manuals/user/user.html