943,716 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 9885
  • JSP RSS
Dec 22nd, 2008
0

Special Characters in JSP

Expand 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.
Last edited by sgbender; Dec 22nd, 2008 at 3:26 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sgbender is offline Offline
1 posts
since Dec 2008
Dec 23rd, 2008
0

Re: Special Characters in JSP

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.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Dec 23rd, 2008
0

Re: Special Characters in JSP

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
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 23rd, 2008
0

Re: Special Characters in JSP

Click to Expand / Collapse  Quote originally posted by sgbender ...
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?
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 24th, 2008
0

Re: Special Characters in JSP

Problem with special characters, you have to remove special and insert ISO-8859-1 format characters

Quote ...
" = &quot;
< = &lt;
like

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

JSP Syntax (Toggle Plain Text)
  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.
Reputation Points: 9
Solved Threads: 1
Newbie Poster
rockonjava is offline Offline
5 posts
since Dec 2008
Jan 15th, 2009
0

Re: Special Characters in JSP

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
Reputation Points: 10
Solved Threads: 1
Light Poster
dreamer14 is offline Offline
31 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: JSp Program to retrive Data from Database
Next Thread in JSP Forum Timeline: A Picture drop down list ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC