• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP Output Question

 
Mike J. Beaty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens that makes the following code not print "java" and "NO" 10 times?
<% for(int i=0;i<10;i++) %>
<%= "java" %>
<BR>
<%for(int i=0;i<10;i++) %>
YES

<BR>
<%for (int i=0;i<10;i++)%>
<%out.println("NO"); %>
That is what does the translator do to the 2 lines of scriptlets/expressions in a row?
Thanks,
 
Aaron Allston
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<% for(int i=0;i<10;i++) %>
<%= "java" %>
<BR>
<%for(int i=0;i<10;i++) %>
YES
<BR>
<%for (int i=0;i<10;i++)%>
<%out.println("NO"); %>
you will get (tomcat 4.1.24):-
java
YES YES YES YES YES YES YES YES YES YES
NO
if you want :-
javajavajavajavajavajavajavajavajavajava
YES YES YES YES YES YES YES YES YES YES
NO NO NO NO NO NO NO NO NO NO

you need to change your code!
<% for(int i=0;i<10;i++) %><%= "java" %>
<BR>
<%for(int i=0;i<10;i++) %>
YES
<BR>
<%for (int i=0;i<10;i++)%><%out.println("NO"); %>
 
Mike J. Beaty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense but my question is what is put in the actual java code that produces this output. For example if you had:

This would print Maybe 10 times. So, what does the translator put between the for loop and the next expression/scriptlet:

Does it add in the brackets after the for loop if they are not typed?
thanks
 
Louise Haydu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how the jsp was translated using Tomcat 4.1
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was something about this in the Wrox certification book which unfortunately I don't have with me. I didn't really understand it then and it puzzled me in the same way it has Mike. At the time I just kept on reading and figured I'd come back to it at another time. I guess the time is now.
I wish I had time to test it right now but I don't. So I'm just going to add some questions. It looks to me like the translation puts in begin and end code brackets if you don't. But unless the next line is template text it puts those brackets on the same line as the iteration code. So we get:

if the next line is not template text.
But if the next line is template text then we get

and so we get the iteration results that we expect.
That's anyway the way it looks to me. If I had time I'd test the following:

I'm betting that in this instance we would get the intended iteration and java would print 10 times.
In any case it's an interesting question and I hope someone can clarify it.
Ken
 
Mike J. Beaty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it is the "\r" and "\n" that do it. What are the rules for the insertion of these charaters? Is the \r and \n put in after every line that ends with a carriage return?
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took a look at the reference to this in Professional SCWCD Certification (page. 189) and it says that:
"An important point to remember with scriptlet code is that all whitespace is preserved in page implementation class by the JSP engine. Although this sounds relatively harmless let's consider a simple if/else scriptlet bock that will cause a fatal compilation error.."
In the example that follows a "out.write("\r\n") is inserted when the else clause is not on the same line as the closing bracket of the if clause. I guess the compilation error is that there is now an else clause unconnected to an if clause This isn't the same as your example but it looks to me as though they're saying that you have to be careful about white space in scriptlet blocks because it can be interpreted as "\r\n".
The book also refers to the keeping all scriplet code in condensed format per the Sun Java coding standards.
Well, I'm sad to say that's just about as puzzling on second read as it was on first read. And I could find no reference to it in the Deshmukh book. So unless anyone can enlighten us it looks to me like what I said yesterday was correct, though I've not seen that anywhere in writing and have come to that perhaps erroneous conclusion just by looking at the results produced by the code.
I'm quite unsatisfied with my answer but I really don't know what more to say. Hopefully someone else can clarify this.
Ken
 
Louise Haydu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing that I can come up with is the following quote:
Be sure to use the braces characters, {}, to enclose if, while, and for statements even if the scope contains a single statement. You can enclose the entire statement with a single scriptlet tag. However, if you use multiple scriptlet tags with the statement, be sure to place the opening brace character, {, in the same statement as the if, while, or for keyword.

Based on this quote, I'm assuming that if an opening brace is not on the same script as the if statement that the translator creates a blank line as the body of the statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic