Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

why println doesnt work in JSP?

 
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I write java code in simple java program as below


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


& 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 ]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mandar Khire ,

Try this way:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
for(int i=0 ; i<=10 ; i++){
out.println(i+"<br>");
}
%>
</body>
</html>
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Printstream.java i read

in same

So when we write (If we write this 2 times)System.out.println("abc");
we get result
abc
abc
In again adition i read following

public abstract void println()
throws java.io.IOException
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').
Throws:java.io.IOException -
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 ]
 
Sheriff
Posts: 67583
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Plain and simple: carriage returns are not significant in HTML.
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic