• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tags stored in database appear literally

 
Dave Mere
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

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?

Thanks, Dave
 
Dave Mere
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hang on that's not how the source code appears at all! I'm adding a space between the characters to avoid that effect:



Hope that's clearer
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are adding spaces while inserting into Database, you have to remove those spaces while displaying.
 
Dave Mere
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added those spaces just for the post. The text in the database contains a standard tag : <I> Salvia </I>

It appears in the generated source code as:
& l t ; I & g t ; Salvia & l t ; / I & g t ;

I have added spaces for this post otherwise the text appears as <I>Salvia</I>. It's something to do with the way the browser is reading the symbols I think.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Mere
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

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!

Thanks for you patience! Been a long week . . .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you emitting the values on the JSP page?
 
Dave Mere
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're on the right track Bear; I've figured it out and it is indeed to do with the method of output. In this case I needed to set the escapeXml attribute of the c ut tag, thus:



Thanks for your help everyone
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic