• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on an Jsp file

 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following jsp code:
<html>
<body>
<% String a = "aaa"; %>
<%! String a = "AAA"; %>
<% String b = "bbb"; %>
<%! String b = "BBB"; %>
<% out.println(a+b); %>
</body>
</html>



What will be the output?
Can anybody tell me why the output is aaabbb rather than AAABBB?
Please explain?
With regards,
Padma priya N.G.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amol,
I think there is no output as there is no println statements.
With regards,
Padma priya N.G.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi padmapriyagururajan:



Here it will print aaabbb since the SOP(a + b) will take the local a and b

Similarly variables declared in <%! %> have instance scope
and declared in <% %> have local scope.
 
Sahid Khan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes output will be aaabbb.
To know why you need to understand the difference between <% %> and <%! %>
<% %> is scriplet which appears inside jspService method as it is. Whereas <%! %> is a declaration which acts like instance variable declaration. So here you hide the variable "a" and "b" locally.
In fact compiled java file will look like..


HTH,
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amol,
Thanks for the answer.
With regards,
Padma priya N.G.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry it was a incomplete post, posted by mistake . Here is the complete post.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amol,
It is not an problem.
With regards,
Padma priya N.G.
 
vidhya jayapalan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Padma priya,

when the JSP file gets translated to servlet

the variables in declaration element <%! ... %> becomes instance variables to the class.


<%! String a = "AAA"; %>
<%! String b = "BBB"; %>

and

the variables in Scriptlet element <% ... %> becomes local variables to the in built service method.


<% String a = "aaa"; %>
<% String b = "bbb"; %>


and

the scriptlet element <% out.println(a+b); %> will be in service method.

Since the local and instance variable names are the same, the local variables overrides instance variables.

Hence, aaabbb is the output.


Thanks,
Vidhya
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vidhya,
Thanks for the nice explanation.
With regards,
Padma priya N.G.
 
Sundar Ram
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vidhya jayapalan:
Padma priya,

Since the local and instance variable names are the same, the local variables overrides instance variables.


A correction, variables cannot be overridden, they can only be shadowed.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sundar,
Do you mean to say that the local variables shadow the instance variables in this case?
With regards,
Padma priya N.G.
 
vidhya jayapalan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sundar for the correction !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic