wood burning stoves 2.0*
The moose likes JSP and the fly likes pre tag causes text not to appear? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "pre tag causes text not to appear?" Watch "pre tag causes text not to appear?" New topic
Author

pre tag causes text not to appear?

verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Hello,
I have the following code displayed:

However, no text is displayed on the page. I'm sure it has to do with the first character inside the <pre> tag is '<'. Is this a known issue? Is there a utility to convert '<' to the literal of '&lt;' and '>' to the literal of '&gt;'? Your prompt reply is greatly appreciated.
[ September 24, 2003: Message edited by: verduka fox ]
[ September 24, 2003: Message edited by: verduka fox ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

It most certainly has to do with the < character. As you have surmised you need to use the &lt; entity. If the value is the result of a JSP substitution, it's easy to write a utility that converts character like <, >, & and the like to their entity references. I know of no standard Java class that performs this function.
hth,
bear
[ September 24, 2003: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Bear,
Thanks for your response. I thought that by using the pre tag that the contents of the tag were displayed exactly as written. For example, multiple spaces are not consolidated as a single space; they are all written as spaces. Therefore, I assumed that the other characters in the pre tag would be observed in the same manner. False assumption?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Yes, while the pre tag will preserve new lines and spaces, and render the content in a fixed-width font, you must still use character entities for special HTML characters such as the angle brackets and the ampersand.
bear
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Ok, so now I'm trying to develop a method to parse through a String, detect any ampersand or angle brackets and change them to the special values. This seems to work but doesn't seem very efficient. Is there a better way to implement this? I look for the ampersand first, since putting it last would find the ampersands in the special values for the angle brackets.
Dorothy Finkel-Laverty
Ranch Hand

Joined: Nov 24, 2001
Posts: 51
Verduka - take a look at the String class' "replace" methods - you can probably handle this in one or two lines of code.
Shabbir Rahman
Greenhorn

Joined: Feb 18, 2002
Posts: 18
Are you searching for following type of code

[ September 24, 2003: Message edited by: Shabbir Rahman ]

-----------------<br />IBM XML Developer <br />SCJP 1.4
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Dorothy and Shabbir,
Thanks for your suggestions. Dorothy, I can't use the replace method because this method requires a char to be replaced with a char. I need a char to be replaced with a String (i.e. ;<' replaced with "&lt;"). Shabbir, I'm using WebSphere Application Server 4.0.3 which only supports 1.3. Unfortunately, the replaceAll method is in 1.4.
Do you have any other suggestions?
One more question: On the same page as the code that I originally questioned is this code:

This displays without a problem. Is there a reason that the first caused a problem? Is it because the character after the < is a letter instead of a number that it causes a problem?
Your suggestions are greatly appreciated.
Shabbir Rahman
Greenhorn

Joined: Feb 18, 2002
Posts: 18
The following is the replaceAll code of String class of 1.4

So you can easily use the following code in your class

Before doing that you need to import java.util.regex.Pattern. I don't know whether 1.3 has regular expression package or not.
[ September 24, 2003: Message edited by: Shabbir Rahman ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

I don't know whether 1.3 has regular expression package or not.

It does not. To use regular expressions under 1.3 you might want to check out the Jakarta regex project. I use it regularly.
hth,
bear
 
GeeCON Prague 2014
 
subject: pre tag causes text not to appear?