aspose file tools*
The moose likes JSP and the fly likes tags stored in database appear literally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "tags stored in database appear literally " Watch "tags stored in database appear literally " New topic
Author

tags stored in database appear literally

Dave Mere
Ranch Hand

Joined: Oct 25, 2004
Posts: 37
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

Joined: Oct 25, 2004
Posts: 37
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

Joined: Sep 16, 2004
Posts: 3636
If you are adding spaces while inserting into Database, you have to remove those spaces while displaying.


My blood is tested +ve for Java.
Dave Mere
Ranch Hand

Joined: Oct 25, 2004
Posts: 37
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

Joined: Mar 22, 2000
Posts: 12805
    
    5
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

Joined: Oct 25, 2004
Posts: 37
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

Joined: Jan 10, 2002
Posts: 61424
    
  67

How are you emitting the values on the JSP page?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dave Mere
Ranch Hand

Joined: Oct 25, 2004
Posts: 37
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

Joined: Jan 10, 2002
Posts: 61424
    
  67

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

Joined: Mar 22, 2000
Posts: 12805
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tags stored in database appear literally