Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

simple jsp question (out.println)

Thread view: 
Frances - 01 Dec 2005 15:12 GMT
out.println does not print a "carriage return" at the end, i.e.,
out.println is behaving the same as out.print..

if I do..

    String token = "one|two|three|four|five";
    StringTokenizer st = new StringTokenizer(token, "|");
    while(st.hasMoreTokens()) {
    out.println(st.nextToken());
    }

output is:

one two three four five

thanks for any suggestions...

Frances
klynn47@comcast.net - 01 Dec 2005 15:20 GMT
Have you tried printing a "<br>" after each line?
Frances - 01 Dec 2005 15:26 GMT
> Have you tried printing a "<br>" after each line?

yes, of course, but my point is shouldn't out.println give you a line
return?  (also it's a bit of a drag to have to do '+ "<br>"' every time
when you're just trying to debug and have several out.println's close
together..)

if in a servlet or stand-alone class I do

    System.out.println

it works fine as expected, only in jsp's it doesn't work as expected..
thank you..
Oliver Wong - 01 Dec 2005 16:13 GMT
>> Have you tried printing a "<br>" after each line?
>
[quoted text clipped - 9 lines]
> it works fine as expected, only in jsp's it doesn't work as expected..
> thank you..

   You can write your own wrapper function:

public static void FrancesPrintln(String output) {
 System.out.println(output + "<br />");
}

   If you're using this for debugging though, you might want to look into
Loggers.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/logging/Logger.html

   - Oliver
Chris Smith - 01 Dec 2005 17:12 GMT
> yes, of course, but my point is shouldn't out.println give you a line
> return?  (also it's a bit of a drag to have to do '+ "<br>"' every time
[quoted text clipped - 6 lines]
>
> it works fine as expected, only in jsp's it doesn't work as expected..

You're confusing two things.  System.out.println prints to the console,
and newlines work fine whether you're doing so from a JSP or not.  In a
JSP, thought, out.println prints to the resulting HTML page.  What you
print is interpreted as HTML, of course, so a newline doesn't show up.

This has nothing to do with JSP versus servlets.  It has to do with
writing to System.out versus the stream from the servlet response.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 01 Dec 2005 18:15 GMT
>yes, of course, but my point is shouldn't out.println give you a line
>return?  (also it's a bit of a drag to have to do '+ "<br>"' every time
>when you're just trying to debug and have several out.println's close
>together..)

the problem is browsers ,in rendering HTML, treat \n just like space.
If you want a line brake you must be a <pre> section or use <br>\n
instead. If you look at your generated html with view source, you
should see your \n just fine.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Smith - 01 Dec 2005 15:22 GMT
> out.println does not print a "carriage return" at the end, i.e.,
> out.println is behaving the same as out.print..

Since your subject line mentions JSP (btw, it'd be a good idea to
mention that in the article body as well), I'll assume you're outputting
HTML.  The println statement is, indeed, writing newlines.  The problem
is that they aren't significant in HTML.  The web browser is ignoring
them.  To confirm that the newlines are indeed there, choose "View
Source" from your web browser when you test the page.

To get HTML to render content on a new line, you need an HTML tag like
<br>, rather than just a newline character.  For that matter, you may as
well avoid out.println and use JSP markup instead.  Something like this:

   <%
     String token = "one|two|three|four|five";
     StringTokenizer st = new StringTokenizer(token, "|");
     while(st.hasMoreTokens())
     {
   %>
         <%= st.nextToken() %><br>
   <%
     }
   %>

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Oliver Wong - 01 Dec 2005 16:10 GMT
>> out.println does not print a "carriage return" at the end, i.e.,
>> out.println is behaving the same as out.print..
[quoted text clipped - 5 lines]
> them.  To confirm that the newlines are indeed there, choose "View
> Source" from your web browser when you test the page.

   Another way to verify that the println statements are indeed writing
newlines is to surround your output with the <PRE> and </PRE> tags, which
cause the browser to temporarily consider whitespace to be significant.

   - Oliver
Frances - 01 Dec 2005 16:50 GMT
>>out.println does not print a "carriage return" at the end, i.e.,
>>out.println is behaving the same as out.print..
[quoted text clipped - 20 lines]
>       }
>     %>

ok, I see what happens then..  browser ignores println's (I mean treats
them like just 'print') from jsp's but not from servlets.. oh well..
thank you all for your responses...  Frances
David  Wahler - 01 Dec 2005 19:13 GMT
> ok, I see what happens then..  browser ignores println's (I mean treats
> them like just 'print') from jsp's but not from servlets..

If that's happening, it's had nothing to do with servlets vs. JSPs.
It's probably because your servlet is sending a content-type header of
"text/plain", while your JSP has a content-type of "text/html". If you
want to do a JSP that outputs plain text without messing with HTML
formatting, you can try putting this line at the top:

<%@ page contentType="text/plain" %>

-- David


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.