I have an application that read text fields from an embedded database then passes these fields to a jsp for output. One requirement is that certain section of the text must appear in italics (latin plant names). My solution to this was to add a function whereby the users select the section of text to italicise, then my data management tool (an Access front end) adds <I> tags to the text stored in the database. So the stored text looks like:
This is one of over 900 species of <I>Salvia</I> that are found throughout the world.
Unfortunately, instead of outputting italicised text, the resulting jsp displays the tags literally as above - no italics, just the actual tags displayed. When I view the source code it look like this:
What's going on? How do I compensate for this?
Joined: Oct 25, 2004
Hang on that's not how the source code appears at all! I'm adding a space between the characters to avoid that effect:
It's something to do with the way the browser is reading the symbols I think.
Exactly right. You will have to convert the "character entities" into the real < and > characters before sending the response. You can use the java.util.regex package classes to compile a "Pattern" representing the sequence of characters and apply it to the String before sending it. For example:
Where ltSRep is a Pattern for the & lt ; sequence. Bill
Joined: Oct 25, 2004
I'm afraid I'm not quite with you! So you're saying the "<" and ">" symbols aren't actually stored like that in my database? When I System.out.println them they look like that. Why does the IE source code read them differently?
For the code you provide, two questions;
Is ltSRep a string variable with the value "<"? Shoudl matcher actually be "matches"? That's the only option eclipse gives me!
Yeah, that's what I suspected. Without that the HTML characters will be converted to their entity equivalents. Usually this is what you want so that you avoid HTML injection issues with user-entered strings. But in the case where you want the HTML to be emitted without encoding, you need the escapeXml attribute.
Author and all-around good cowpoke
Joined: Mar 22, 2000
I see that Bear has zeroed in on your problem, but to tie up loose ends, let me answer this.
Is ltSRep a string variable with the value "<"? Shoudl matcher actually be "matches"?
No, ltSRep is a Pattern - see the java.util.regex package. That line of code creates a Matcher from the pattern and the tmp String and that Matcher executes replaceAll - every instance of the pattern in the string is replaced with "<" - a remarkably fast operation. Bill