Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
| Search | Today's Posts | Mark Forums Read
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 21st, 2006, 01:51 AM
Registered User
 
Join Date: Apr 2005
Location: Udupi, Karnataka, India.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Janardhana M
Default Special Character issue in JSP

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
 
Old July 1st, 2006, 07:19 PM
Friend of Wrox
 
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old July 2nd, 2006, 11:54 PM
Registered User
 
Join Date: Apr 2005
Location: Udupi, Karnataka, India.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Janardhana M
Default

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
 
Old July 3rd, 2006, 01:23 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old July 3rd, 2006, 01:59 AM
Registered User
 
Join Date: Apr 2005
Location: Udupi, Karnataka, India.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Janardhana M
Default

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
 
Old July 12th, 2006, 04:46 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old July 12th, 2006, 07:27 AM
Registered User
 
Join Date: Apr 2005
Location: Udupi, Karnataka, India.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Janardhana M
Default

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_B gColor;
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
 
Old July 12th, 2006, 02:14 PM
Friend of Wrox
 
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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...IComponent.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/
 
Old July 12th, 2006, 11:21 PM
Registered User
 
Join Date: Apr 2005
Location: Udupi, Karnataka, India.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Janardhana M
Default

Thanks Jon.
I will try out this method. Also thanks for the links given.

Janardhana




Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation for special character nazneen Java GUI 5 February 13th, 2006 03:13 PM
Decimal code for a special character mcinar HTML Code Clinic 14 February 4th, 2005 01:56 PM
Special Character mg1966 Classic ASP Databases 2 December 13th, 2004 11:12 AM
XML - Special Character Formatting sean1230 XML 3 February 18th, 2004 05:17 AM
HELP!!! the problem of handling special character kemin Classic ASP Databases 1 December 13th, 2003 01:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.