File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How to display plain text in jsp without using pre, xmp, or code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to display plain text in jsp without using pre, xmp, or code" Watch "How to display plain text in jsp without using pre, xmp, or code" New topic
Author

How to display plain text in jsp without using pre, xmp, or code

George Joseph
Greenhorn

Joined: Nov 05, 2001
Posts: 24
Hi Java Ranchers,
Is there any method in jsp to encode the HTML so that I can display HTML tags say <b> <input type=text> ...etc
I don't want to use
<pre></pre> or
<xmp></xmp> or
<code></code>
as all these methods have limitations like horizontal scrolling etc.
There is a method URLEncode but that also does not serve my puropse
regards
George Joseph
Joe Gilvary
Ranch Hand

Joined: May 11, 2001
Posts: 152
It sounds like you're creating a "tutorial" style page,
teaching people to use HTML. Of course, you need some
way to display valid HTML source in such pages.
You could use the entity codes for the ampersand, greater
than, less than, etc.
Show the <b> tag as <b>
The others work the same way.
< <
> >
& &
Hope that helps.
Joe
George Joseph
Greenhorn

Joined: Nov 05, 2001
Posts: 24
Hello Joe,
Do you mean that I have to replace all
"<" with "&lt;";
">" with "&gt;";
"\n" with "<BR>";
Is there no other automated method in JSP to ENCODE Html so that I can display plain text from my database as it is.

George

Originally posted by Joe Gilvary:
It sounds like you're creating a "tutorial" style page,
teaching people to use HTML. Of course, you need some
way to display valid HTML source in such pages.
You could use the entity codes for the ampersand, greater
than, less than, etc.
Show the <b> tag as <b>
The others work the same way.
< <
> >
& &
Hope that helps.
Joe

[This message has been edited by George Joseph (edited November 07, 2001).]
[This message has been edited by George Joseph (edited November 07, 2001).]
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
You may set content type in the jsp
<%@ page contentType="text/plain"%>
or create a method to translate all the html characters.
<%! public String translateHtmlChars( String string ){
}
%>
What is the problem you have with xmp ?
Happy coding
Originally posted by George Joseph:
Hi Java Ranchers,
Is there any method in jsp to encode the HTML so that I can display HTML tags say <b> <input type=text> ...etc
I don't want to use
<pre></pre> or
<xmp></xmp> or
<code></code>
as all these methods have limitations like horizontal scrolling etc.
There is a method URLEncode but that also does not serve my puropse
regards
George Joseph

George Joseph
Greenhorn

Joined: Nov 05, 2001
Posts: 24
Hello Zkr Ryz,
The problem with <xmp> is that is cause horizontal scrolling that is
If I enter in a textarea without new line (Enter key)then when I display using <xmp></xmp> the webpage will scroll horizontally that is the text won't be wrapped.
I want the plain text to be shown as it is without scrolling.
Have you any idea how this discussion/Messgae board works.
Here plain text is shown as it is without unwanted horizontal scrolling.

George

Originally posted by Zkr Ryz:
You may set content type in the jsp
<%@ page contentType="text/plain"%>
or create a method to translate all the html characters.
<%! public String translateHtmlChars( String string ){
}
%>
What is the problem you have with xmp ?
Happy coding

Joe Gilvary
Ranch Hand

Joined: May 11, 2001
Posts: 152
When I look at a page source, I see the codes I
entered in my earlier message are encoded as
entity codes. Each post is strung out in the
HTML source as a looong line of output.
My guess is that the text is captured and converted
to use the entity codes, and the result is stored.
That result is fetched out for display when one of
use browses a thread.
So, the input I used to show the entity codes failed
above, and my earlier message doesn't display them
properly. (I didn't encode the ampersand at the start
of the entity code as you did.) It looks like you'll
need to write that method that Zkr Ryz suggests. Encode
all the codes that need to pass through the same way
you did the codes in your reply to my message.
I did something like that once, with XML inside a SOAP
method call on its way to/from an Oracle database. I
found I needed to use the numeric entity codes, but I
can't remember right now which component had trouble with
which specific entity code. If you're encoding anyway,
and don't have a driving need to let people read the input
from an SQL prompt, you might be better off with the
numeric codes.
Sorry about the mess up in my first post.
Thanks,
Joe
George Joseph
Greenhorn

Joined: Nov 05, 2001
Posts: 24
Hello Joe,
In ASP (Active Server Pages) there is a method called Response.HTMLEncode(html String), which will encode all < to &lt; and so on.
According to Mr. Zkr Ryz , I will have to write my own method say
<%! public String translateHtmlChars( String string ){
}
%>
But If anybody has written such a method in JSP let me know so that I can reuse the code as it will be a great help.

George

Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Hi,
I am new to Java and had a similar problem. I wrote a little class I use to convert certain characters and it works fine for my app so I'll post it here and it may be useful to you as well.

Hope this helps,
Pat
George Joseph
Greenhorn

Joined: Nov 05, 2001
Posts: 24
Hello Pat,
Thanks for your code, I also appreciate your coding style and the selection of variable names.

George
Originally posted by Pat Wallwork:
[B]Hi,
I am new to Java and had a similar problem. I wrote a little class I use to convert certain characters and it works fine for my app so I'll post it here and it may be useful to you as well.

Hope this helps,
Pat[/B]

Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
Hey Guys!
I have a similar problem mentioned in this old post. I have created a method parseString(). Does anybody know if a similar thing can be achieved by jsp page directive attribute pageEncoding.

Thanks,
Martin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display plain text in jsp without using pre, xmp, or code
 
Similar Threads
sending/retrieving data via SQL statements...
is param implicit object bean or Map(EL)?
Escaping special characters
How to link a text file for download from a JSP page...
Vertical alignment issues : vertical-align:middle does not work