This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one question about JavaBean

 
julie li
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 22
Question for Java Beans.
The contents of beans class are as follows:-
package projsp;
public class trying
{
private String name;
private int age;
public void setName(String n)
{
name=n;
}
public void setAge(int a)
{
age=a;
}

public int getAge()
{
return age;
}
public String getName()
{
return name;
}
}
The contents of test.jsp file are as follows:
<%@ page language="java" import="projsp.*" %>
<jsp:useBean id="test" class="projsp.trying" />
<html>
The Name is <getProperty name="test" property="name" />
The Age is < jsp:getProperty name="test" property="age" />
</html>
If a user types in http://servername:8080/test.jsp?name=Anand&age=22 the out put is
A The Name is Anand
The Age is 22
B The Name is Null
The Age is 0

C It gives a compilation error as the
properties are not set prior
to retrieving them
D The Name is null
The Age is null


I think the correct answer is B.
But the Question Auther choose D.
Could anybody help to explain it to me?
Thanks a lot !!!
 
boyet silverio
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi julie,
it is because you haven't set the properties first before getting the property values. You should first set it to something like <jsp:setProperty name="test" property="*" />. This would get the parameters attached to your address and assign the corresponding values to the properties of the bean i.e. test, being accessed.
Since the properties haven't been set, and since these are instance variables, they would be assigned default values, e.g. null for String.
It would be better if you whip up some code, run it, and see the results. Change the code for other possibilities e.g. initialize the instance variables of your bean class and see what happens.
 
Enrico Mannarino
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boyet,
age is declared as int! And name as String!
The default values for these instance variables should be:
name = null
age = 0
So the right answer should be b)!
I think we have another thread on this somewhere!
E,rico
 
boyet silverio
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Enrico,
Thanks, you're right. i might have jumbled the code or mixed up the results while testing different variations.
 
julie li
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi E,rico and Boyet,
Thanks for your help. I have passed the SCWCD exam with score 86%
Best Regards
julie
 
boyet silverio
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations on your efforts, Julie.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic