|
Subject:
|
Special Character issue in JSP
|
|
Posted By:
|
Janardhana M
|
Post Date:
|
6/21/2006 1:51:14 AM
|
How to handle the special characters like %,(Option+' in Mac),(Option+" in Mac),+,& etc entered within a string and passed on to another page, where it is retrieved using request.getParameter(). It comes as '?' character or empty space. Also how to save such values into MySQL database so that again while displaying it after fetching from database will be proper.
Janardhana
|
|
Reply By:
|
panacea
|
Reply Date:
|
7/1/2006 7:19:09 PM
|
Janardhana:
The reason those characters turn into question marks is because they're not representable in the character set you're using. The best way to fix this is to specify UTF-8 encoding everywhere you get a chance. At the HTTP level, you do this in the Content-Type header. For example:
Content-Type: text/html; charset=UTF-8
You can also specify UTF-8 encoding on your <form> tags.
At the Java level, specify the encoding by calling request.setCharacterEncoding("UTF-8") before you read any paramters. And at the MySQL level, specify that your database table uses UTF-8 encoding. You pass this at the end of CREATE TABLE statements, or you can use ALTER TABLE to modify the encoding of an existing table.
Once you're using UTF-8 at all levels of your application, characters will never become corrupted or turn into question marks. If you run into problems, use a debugger and see where the corruption is happening. Then make sure both sides are using UTF-8 and you'll fix the problem.
Jon Emerson http://www.zoominfo.com/JonEmerson
|
|
Reply By:
|
Janardhana M
|
Reply Date:
|
7/2/2006 11:54:53 PM
|
Thanks a lot for the solution.
But we are already using the said UTF-8 character encoding for Content type, setCharacterEncoding and MySQL table. Only thing is we have not used for <form> tags. Also the problem is worse when we use AJAX to submit the page.
Janardhana
|
|
Reply By:
|
panacea
|
Reply Date:
|
7/3/2006 1:23:16 AM
|
If the problem's worse with AJAX, are you using a Javascript function for encoding string vars on the client side into UTF-8 before they're passed to the server? What function are you using -- are you sure it's doing a UTF-8 encoding and not just an ASCII encoding? What method (POST, GET?) are you using to upload the data?
Jon Emerson http://www.jonemerson.net/
|
|
Reply By:
|
Janardhana M
|
Reply Date:
|
7/3/2006 1:59:04 AM
|
Yes we are using Javascript function to submit the xmlHttp request for completing the AJAX request, using POST method. And we are using the command
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
before sending the request to another page. I hope this will handle the UTF-8 character encoding. Does we need to do something more?
Janardhana
|
|
Reply By:
|
panacea
|
Reply Date:
|
7/12/2006 4:46:13 AM
|
How are you calculating what URL to submit to? Does your URL calculation include a function for appending query parameters?
Jon Emerson http://www.zoominfo.com/JonEmerson
|
|
Reply By:
|
Janardhana M
|
Reply Date:
|
7/12/2006 7:27:58 AM
|
Yes I'm calculating how the URL parameters must be sent in a way as shown below
the_Param="Path="+the_Path+"&Greet="+the_Greeting+"&Type="+the_Fonttype+"&Msg="+the_Message+"&Lay="+the_Layout+"&Col="+the_Fontcolor+"&BgColor="+the_BgColor; xmlHttp=GetXmlHttpObject(isTraial); xmlHttp.open("POST","CallPreview.jsp",true); xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8") xmlHttp.send(the_Param);
and sending it as a xmlHttp request to another 'CallPreview.jsp' page as shown above.
Janardhana
|
|
Reply By:
|
panacea
|
Reply Date:
|
7/12/2006 2:14:53 PM
|
A-ha! That's exactly your problem. You need to use a JavaScript function to UTF-8 encode each of those parameters. For example,
the_Param= "Path=" + encode(the_Path) + "&Greet=" + encode(the_Greeting) + "&Type=" + encode(the_Fonttype) + ...
If you are only interested in supporting modern browsers, then your encode() function can simply be encodeURIComponent(). For more on this function, see http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp
If you would like to support IE5 and NN4, then you must create your own implementation of encode(). Fortunately, the fine people at this web site (http://www.worldtimzone.com/res/encode/) have already done the work for you, so you just need to cut and paste it :).
Jon Emerson http://www.jonemerson.net/
|
|
Reply By:
|
Janardhana M
|
Reply Date:
|
7/12/2006 11:21:23 PM
|
Thanks Jon. I will try out this method. Also thanks for the links given.
Janardhana
|