Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP displaying single and double quotes as symbol

Tags:

java

html

jsp

I have a JSP page retrieving data and when single or double quotes are in the text they are displayed as this symbol .

JSP Code:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>General</title>
    </head>
    <body>
        <h1> <%= order.getDescription %> </h1> 
    </body>
</html>

Example: An order's description should look like this,

"20 - 4" x 6" widgets"

but I am getting this,

"20 - 4 x 6 widgets"

NOTE: I can not make modifications to the database.

[ EDIT ]

I used the commons-lang-2.4.jar to escape the characters and these are the primary characters giving me trouble:

  1. &#145 -> ‘
  2. &#146 -> ’
  3. &#147 -> “
  4. &#148 -> ”
  5. &#150 -> –

I am sure other characters in the some format would give me issues, however, I just did a replace on the characters for a temporary fix and I am currently testing the suggestions below.

[ CODE FOR SOLUTION ]

This probably not the best way to do it but it got the job done. The code below is in the backing bean after the the data is retrieved from the database.

description = StringEscapeUtils.escapeHtml(description);

description = description.replaceAll("&#145;", "&quot;");
description = description.replaceAll("&#146;", "&quot;");
description = description.replaceAll("&#147;", "&quot;");
description = description.replaceAll("&#148;", "&quot;");
description = description.replaceAll("&#150;", "-");

description = StringEscapeUtils.unescapeHtml(description);
like image 383
Berek Bryan Avatar asked Sep 04 '09 13:09

Berek Bryan


People also ask

How can we change double quotes in JSP?

The parser used in JspEngine does not allow double quotes within double quotes, instead one of them must be escaped by using single quotes and double quotes or the inner double quotes could also be escaped by using backslash before the double quotes \" for one of them.

What does 2 quotation marks mean in Java?

For instance, in Java a double-quote is a string while a single-quote is a character. Defining char letter = "a" in Java will cause an error in the same way as String s = 'a bunch of letters' will. Always better to use double-quotes when storing strings.

How do you display double quotes in a string?

The basic double-quoted string is a series of characters surrounded by double quotes. If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters.

Is double quote a special character in regex?

Firstly, double quote character is nothing special in regex - it's just another character, so it doesn't need escaping from the perspective of regex. However, because Java uses double quotes to delimit String constants, if you want to create a string in Java with a double quote in it, you must escape them.


2 Answers

That's character U+0094, which is a largely-unused control code. You will usually get characters in this range by accident if you use ISO-8859-1 to decode bytes that are actually in Windows codepage 1252 (Western European). They are similar encodings and often confused with each other, but the symbols in the range 0x80-0x9F are different. Windows cp1252 uses some of those for things like smart quotes, which is what you probably expected here: a double-close-quote (”, U+201D RIGHT DOUBLE QUOTATION MARK).

Such is the confusion that most web browsers, when told that a web page is ISO-8859-1, will actually use cp1252 instead and would render the quote. So this probably isn't a markup-side issue.

What you probably have is a database that contains CP1252, and a data access layer that is converting the bytes out of it to a String using ISO-8859-1 — perhaps because this is the server's default encoding. Ideally you'd want to configure the database to store Unicode strings natively, but if you can't do that you'll need to a way to configure your database connector to use the CP1252 encoding instead of ISO-8859-1. How you do this depends on what you're connecting with and to; you might have to set a property, or include a parameter in a connection string.

If you can't do that with your data layer, about the only thing left is to manually go over all the string values you get from the database and transcode them back to what they should be, by encoding with a ISO-8859-1 Encoding, followed by decoding with CP1252. This would be a real pain to do, but as a last resort would work.

[Side-issue: close-double-quote is the incorrect character for denoting inches. ″ (Unicode U+2033 DOUBLE PRIME) would be best, but if you're limited to legacy encodings, a straight " double-quote will do.]

like image 152
bobince Avatar answered Sep 28 '22 08:09

bobince


These are probably non-standard characters in your database...perhaps directional quotes instead of the straight up-and-down ones?

A straight-forward way to handle this, since you can't change the data in the database, would just be to use a replace or regex to swap out "bad" characters with ones that will display correctly.

like image 24
Beska Avatar answered Sep 28 '22 09:09

Beska