Special Characters in JSP

Reply

Join Date: Dec 2008
Posts: 1
Reputation: sgbender is an unknown quantity at this point 
Solved Threads: 0
sgbender sgbender is offline Offline
Newbie Poster

Special Characters in JSP

 
0
  #1
Dec 22nd, 2008
Hi,

I am kind of new to JSP. I am having trouble outputing something:

<%=request.getParameter("UserName")%>

now if UserName has special Characters (i.e &hearts; ) it does not get outputed corectly. that code above should display the heart symbol, but instead I get something like this: âÂ...

is there some kind of converstion function I can use?

Thanks.
Last edited by sgbender; Dec 22nd, 2008 at 3:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Special Characters in JSP

 
0
  #2
Dec 23rd, 2008
This isn't a JSP issue, the &heart; isn't a standard special character so you need to make sure you page will handle the it. It will be an encoding issue of some sort. Find out which character-set the heart is found in, and make the page use that set.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Special Characters in JSP

 
0
  #3
Dec 23rd, 2008
As sillyboy mentioned, this is not a JSP issue, the "&heart;" symbol is not supported by the Latin1 character set (ISO-8859-1) which most probably by default is the character encoding for your page.

So set change your page encoding to Unicode if you want it to be displayed.

Here
is a guide on how to use special characters in HTML
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,611
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 465
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Special Characters in JSP

 
0
  #4
Dec 23rd, 2008
Originally Posted by sgbender View Post
Hi,

I am kind of new to JSP. I am having trouble outputing something:

<%=request.getParameter("UserName")%>

now if UserName has special Characters (i.e &hearts; ) it does not get outputed corectly. that code above should display the heart symbol, but instead I get something like this: âÂ...

is there some kind of converstion function I can use?

Thanks.
The document encoding doesn't seem to be a problem here since &hearts; is a valid HTML entity and its rendering doesn't seem to be dependent on the document encoding. The specification says: "To support these entities, user agents may support full [ISO10646] or use other means".

How exactly are you persisting the user name to your database i.e. where exactly is the user name coming from? Have you tried to print out the user name before persisting it? Is it the same as inputted by the user? Does directly writing out "&hearts;" works?
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: rockonjava is an unknown quantity at this point 
Solved Threads: 1
rockonjava's Avatar
rockonjava rockonjava is offline Offline
Newbie Poster

Re: Special Characters in JSP

 
0
  #5
Dec 24th, 2008
Problem with special characters, you have to remove special and insert ISO-8859-1 format characters

" = &quot;
< = &lt;
like

make a method and put all possible special character which need to replace

  1. <%!
  2. public String convSpecialChar(String htmlString)
  3. {
  4. htmlString = htmlString.replaceAll("\\<.*?\\>", "");
  5. htmlString = htmlString.replaceAll("\r", "<br/>");
  6. htmlString = htmlString.replaceAll("\n", "<br/>");
  7. htmlString = htmlString.replaceAll("\"","&quot;");
  8. htmlString = htmlString.replaceAll("\'","'");
  9. htmlString = htmlString.replaceAll(",",",");
  10. return htmlString;
  11. }
  12. %>
  13. <%=convSpecialChar(request.getParameter("UserName"))%>
Last edited by rockonjava; Dec 24th, 2008 at 5:31 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: dreamer14 is an unknown quantity at this point 
Solved Threads: 1
dreamer14 dreamer14 is offline Offline
Newbie Poster

Re: Special Characters in JSP

 
0
  #6
Jan 15th, 2009
I will recommend validation that prevents users from typing in some keys like ;( who uses this in username)
this can be done by a quick search on google with the key words Char Code. Hope this helps
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC