• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help! Jsp Declaration Question

 
Nadine McKenzie
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please tell me why this code causes a compilation error?
<html>
<body>
<%! int i; %>
<%! i = 12; %>
Value of i is: <%= ++i %>
</body>
</html>
Isn't
<%! int i; %>
<%! i = 12; %>
equivalent to
<%! int i = 12; %>
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%! %> is the declaration inside this you can place methods or anything you can place OUTSIDE a method in a java class. The following is illegal in Java

This is exactly the type of thing that you are trying to do. You can make this right by removing the !
 
tony lee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
<%! int i; %>
<%! i = 12; %>
is not equivalent to
<%! int i = 12; %>
why?
<%! i = 12; %> doesn't satisfy with the defination of a declaration because there's not an identifier for var i. what you intended to do is more like a scriptlet <% i = 12; %>
 
Sohail Nasim
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this one
<%! int i; %>
<% i = 3; %>
<% j = 8; %>
<%! int j = 5; %>
i is <%=i%>
j is <%=j%>
What is the output?
 
Goan Balchao
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should be OK. This is because the declarations are assimilated and declared as instance variables in the page implementation class.
So j is available to the code before it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic