• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP scriptlet

 
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
 
zoe dude
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
In second option u r trying to get the value of the variable by using experssions.Even second will be ok like
<%
String a1="A";
String b1="b";
%>
<% if(true) %>
<%= a1 %>
<%else%>
<%= b1 %>
Like this.Whenu have specified like <%= "I am fine "%>,then JSP engine look for a "I am fine",which no way of defining the correct variable.
see the difference between scriplets and expressions.
Very well defined in JSP Specfication.
 
Peter Masreblta
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi zoe,
Thanks for your response.
The error I got from 2) was:
Generated servlet error:
C:\Tomcat4.0\jakarta-tomcat-4.0\work\localhost\scwcd\jsp\test$jsp.java:296: 'else' without 'if'.
else
 
zoe dude
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
<% if(true)
{
%>
<%= "I am fine" %>
<%
}
else
{
%><%= "You are fine" %>
<%
}
%>
Ya even i get the same error.but if we mofiy our code like this then it work fine.
I am still looking on your original code but not able to get why it's an giving an error. can someone explain pls.
Strange to see
<% if(true) %>
hello
<%else%>
bye
this works fine but when we put expressions,this gives and error.Can some one explain this pls.
[This message has been edited by zoe dude (edited November 21, 2001).]
[This message has been edited by zoe dude (edited November 21, 2001).]
 
Win Yu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be ok if you do like this. I tested it already.
<% if(true){ %>
<%= "I am fine" %>
<% } else {%>
<%= "You are fine" %>
<%}%>
if you use your code:
<% if(true)%>
<%= "I am fine" %>
<% else %>
<%= "You are fine" %>
The JSP will be translated to following java code, it will not be compiled sucessfully.I think that is because extra code are added by JSP container during translation . So if we use "{" and "}", It will works.
if(true)
out.write("\r\n");
out.print( "I am fine" );
out.write("\r\n");
else
out.write("\r\n");
out.print( "You are fine" );
the error message is :'else' without 'if'.
else
^

[This message has been edited by Win Yu (edited November 21, 2001).]
[This message has been edited by Win Yu (edited November 22, 2001).]
 
Peter Masreblta
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Win,
Thanks for your response.
The question is why
<%= "I am fine" %> generates:
out.write("\r\n");
out.print( "I am fine" );
out.write("\r\n");
but "I am fine" generates:
out.write("\r\nI am fine\r\n");
 
zoe dude
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.
Thanks win for the information.Important from exam point of view.

[This message has been edited by zoe dude (edited November 21, 2001).]
 
Rishi Singh
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi peter,
after the statement <%if(true)%> if u give the expression <%="Iam fine" %> the container expects the the start tag i.e { and the end tag } of the block to be executed if the condition is true, which if it doesnot get it doesnot recognizes the else block.On the other hand if u put <%if(true)%> Iam fine it doesnot takes "Iam fine " as part of the block, rather it just prints it as any template text is printed as it is.
I hope this helps...
 
Peter Masreblta
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following will be fine:
<% if(true) %><%= "I am fine" %><%else%><%= "You are fine" %>
but, this will not:
<% if(true) %> <%= "I am fine" %><%else%><%= "You are fine" %>
[This message has been edited by Peter Masreblta (edited November 21, 2001).]
 
Win Yu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the perfect way is put "{" and "}" when we use "if" condition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic