• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

one question about JavaBean

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!!
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations on your efforts, Julie.
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic