aspose file tools*
The moose likes JSP and the fly likes why println doesnt work in JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "why println doesnt work in JSP?" Watch "why println doesnt work in JSP?" New topic

why println doesnt work in JSP?

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 498

I write java code in simple java program as below

I get result as

& i write same java code in JSP

& i get result as 0 1 2 3 4 5 6 7 8 9

My question is that why println doesn't work in JSP like normal java program?

For getting result like normal java program, in JSP then i should use <br> tag like this:-

I want result should like normal java program, but i dont want to use<br> tag.
I try lot, but i didnt get answer, so i think no option for <BR> tag.
[ July 23, 2008: Message edited by: Mandar Khire ]

Millions saw the apple fall, but Newton asked why.
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

The out.println in JSP is working exactly as it does in other application. If you look at the HTML source code you will see that each number is on a different line.

The difference is that your browser uses HTML to make a view of the code you send it, and in HTML, a new line is signaled by the <br/> tag, not by the \n\r character.

The only way around it would be to use the 'pre'format tags surrounding your loop to get the code to appear on screen as it does in the source code.

vedha vas

Joined: Jul 22, 2008
Posts: 10
Hi Mandar Khire ,

Try this way:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
for(int i=0 ; i<=10 ; i++){

Regrads,<br />Vedha.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 498

AS Steve Luke write
If you look at the HTML source code you will see that each number is on a different line.
because <br> tag.
& as vedha vas give us small but right program.
With referring both & with my original question i am trying to say that:- I read "JavaServer Pages enable you to write standard HTML Pages containing tags that run powerful program based on Java Programing Language".
HTML Tag example <br>
Scripting element Scriplet example is <% java code %>
Now i want if within <% and %> there is java code, then why println() dont give result as give it us in core java program? Within <% & %> there code is no relation with HTML.
AS vedha vas gives example out.println(i+"<br>");
But it is HTML tag within java code.
There are various ways of writing server code that build on top of the basic Servlet classes. JSP and Freemarker let you embed bits of Java code, and other scripting inside your HTML pages. These are parsed and converted to Java Servlet programs that dynamically generate HTML with variable fields. These are automatically recompiled as needed.
This lines i write from here.

In addition
I read following link println()
line Separator
In i read

in same

So when we write (If we write this 2 times)System.out.println("abc");
we get result
In again adition i read following
public abstract void println()
Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n'). -
which is written in this link.

But i didn't found this works like written in bold above.
Same question ask in javaranch forum.
Same ask in sun java forum.
But i didn't found satisfying answer.
By reading so much & didn't get answer so i confuse so much.
[ July 23, 2008: Message edited by: Mandar Khire ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 61226

Plain and simple: carriage returns are not significant in HTML.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: why println doesnt work in JSP?