• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP Scriptlet  RSS feed

 
Peter Masreblta
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
1)
<% if(true) %>
I am fine
<%else%>
You are fine
2)
<% if(true) %>
<%= "I am fine" %>
<%else%>
<%= "You are fine" %>
Can anyone tell me why 1) is fine, but 2) is not?
Thanks

 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I never use if...else in sriptlets without brackets. It can get a bit wierd and unpredicatable.

Thoroughly unreadable, but sometimes I can't help myself.
Doesn't really answer your question but it is an answer of sorts...
Dave
 
Peter Masreblta
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
Thanks for your response.
I knew that putting brackets would make it compile, but why?
Why 2) requires brackets, but 1) does not?
Thanks
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it each way and have a look at the way the JSP is converted to java. The non-bracket way only works if the bit inside the scriptlet evaluates to a single line of java code.
I think the "<%=" version equates to:

where as the first version (because the text gets treated as a single object) comes out as:

The first version (of mine) is wrong, but brackets will fix it. The second version is correct but unstable.
Dave.
 
Peter Masreblta
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
Thanks again for your response.
The following will be fine:
<% if(true) %><%= "I am fine" %><%else%><%= "You are fine" %>
It will generate:
out.print( "I am fine" );
but, this will not:
<% if(true) %> <%= "I am fine" %><%else%><%= "You are fine" %>
It will generate:
out.write(" ");
out.print( "I am fine" );
My question is what the rule for these is?
Thanks


[This message has been edited by Peter Masreblta (edited November 21, 2001).]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would depend on the implementation of the jsp compiler too much to track down.
The only real requirements here are that the JSP conform to the JSP spec and it should get turned into something that conforms to the Servlet spec. I don't think there is any restriction on HOW this is accomplished.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!