This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes JSP out.println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP out.println" Watch "JSP out.println" New topic

JSP out.println

Sujatha Rangarajan

Joined: Jul 19, 2006
Posts: 23
i want to display multiline data retrieved from database as such..with spaces in a jsp page ..but out.println always trims and display using tomcat as there any other way to prevent trimming

thanx in advance,
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

Really ? Wouldn't that be that browser who removes the spaces ?
Try to use nbsp in your println.

(I wouldn't rely on spaces to format the output.)
[ July 19, 2006: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Sujatha Rangarajan

Joined: Jul 19, 2006
Posts: 23
it trims the given string also..what do u mean browser trimming
Sujatha Rangarajan

Joined: Jul 19, 2006
Posts: 23
also data is from database it cannot be predicted whether the data would contain space or not..
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

The out.println method trims nothing.
It only adds a new line character to the end.
(out.print doesn't add a new line).

To better understand Satou's post, try viewing the HTML source from your browser's "Menu -> View -> View Source" option. You will see that all the whitespace is still there.

Browsers ignore whitespace when rendering HTML becuase they are supposed to.
There are HTML tags for spaces and new lines.
Use &nbsp; for a space and <br /> for a line break.
You will need to parse your database results and substitute spaces and line breaks with these symbols.

Two other alternatives are:
1.) Use the <pre> your text </pre> tags.
2.) Display the literal text in a <textarea>your text</textarea> control.
[ July 19, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
It is sorta covered in the JavaRanch Style Guide.
subject: JSP out.println
Similar Threads
Show animation during report building
Display Message when I insert or update record
printing values
problem in reading from checkbox
struts display