• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question ID :1000043338670

 
Win Yu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question ID :1000043338670
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?
I thought it was compile error. but the answer is aabb.Can anyone explain?
 
umesh atada
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please differance b/w tags "<%" and "<% !".
I think that must help u find the answear
------------------
Umesh
 
Rishi Singh
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Win,
When we put something in a scriptlet it goes into either the doGet or the doPost(i.e local to the service method). On the other hand whatever we put in as a declaration goes as instance variables of the generated servlet.In case there is the same variable being declared in the
jsp:declaration and the scriptlet,the local variable overrides the instance variables and it gets preference over it.As a result when we do out.println it gives "aaabbb"
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here the decalration of the variables are in two different ways.
One is with Declaraion tags and other is with Scriplets tags.
With the declaration tag i.e. <%!....%> the variables are initialised when the JSP page is initialised and with page scope and are similar to class variables.
With the scriplet tag, i.e. <%...%> the variables are defined with in a servlet method and this method is executed everytime the request is given to the JSP page.This is similar to method variable.
Hope this solves.
Thanks.


------------------
L Goundalkar
lggoundalkar@hotmail.com
Sun Certified Programmer for Java 2 Platform
 
ruijin yang
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Win,
The correct answer is "out.println(a+b);".Because JSP container translates it into out.println("out.println(a+b);").So you can get output ut.println(a+b);
If you want to get output "aaabbb",you have to put "out.println(a+b);" into scriptlet <%out.println(a+b);%>.
ruijin yang
[This message has been edited by ruijin yang (edited November 19, 2001).]
[This message has been edited by ruijin yang (edited November 19, 2001).]
 
morph wang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ruijin yang:

The correct answer is "out.println(a+b);".Because ...

hahahahaha...
 
morph wang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ruijin yang:

The correct answer is "out.println(a+b);".Because ...

hahahahaha..., what a trick !
 
zoe dude
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correct answer is out.println(a+b);
see this is a tricky question.There r no scriplets for the out.println.....
We all have to be care ful in these kinds of questions....
If we add scriplets then we will get aaabbb.
All the best for future.
[This message has been edited by zoe dude (edited November 19, 2001).]
 
Win Yu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all the reply. I do think the answer should be "out.println(a+b)". That means the Jweb+'s answer is wrong.
I am going to take exam this Friday. Please give me some advice for this final week. Thanks.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3820
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just checked the question and it looks correct to me. That line is <% out.println(a+b); %> and not out.println(a+b);
Can you please check it again?
------------------
SCJP2, SCWCD Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
JQPlus - For SCJP2
JWebPlus - For SCWCD
JDevPlus - For SCJD
 
Win Yu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Anil:
I just checked the question and it looks correct to me. That line is <% out.println(a+b); %> and not out.println(a+b);
Can you please check it again?

Hi Paul: I double checked it. I am sure it has problem.
 
Peter Masreblta
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The question in my copy is:
Question ID :1000043338670
Consider the following jsp code:
<html>
<body>
<% String a = "aaa"; %>
<%! String a = "AAA"; %>
<% String b = "bbb"; %>
<%! String b = "BBB"; %>
<% out.println(a+b); %> <-------- not out.println(a+b);
</body>
</html>
What will be the output?
 
zoe dude
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer will be "aaabbb".
 
Win Yu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, We all understand the difference between "<% out.println(a+b); %>" and "out.println(a+b);" now. Hope I do not make anything wrong in my exam.
[This message has been edited by Win Yu (edited November 21, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic