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.

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.

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

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?

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

<%!
public String convSpecialChar(String htmlString)
{	
	 	htmlString = htmlString.replaceAll("\\<.*?\\>", "");
		htmlString = htmlString.replaceAll("\r", "<br/>");
		htmlString = htmlString.replaceAll("\n", "<br/>");
		htmlString = htmlString.replaceAll("\"","&quot;");
		htmlString = htmlString.replaceAll("\'","&#39;");
		htmlString = htmlString.replaceAll(",","&#44;");
	 	return htmlString;
}
%>
<%=convSpecialChar(request.getParameter("UserName"))%>

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

This article has been dead for over six months. Start a new discussion instead.