• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock question

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this jsp code to compile and run which of the given options should be true?
<jsp:useBean class="com.bookstore.Book" type="java.lang.Object" id="book" />




Options

Select 1 correct option.

1 This statement is wrong as type attribute is invalid.

2Book must have a public no args constructor

3Book must have a public constructor but there is no requirement on arguments.

4Book must have a public getInstance() method.

5This statement will always throw an exception at runtime no matter what you do to Book class.


the answer is 2..
why do they say "must"..if no ctor is defined it means there is a noarg ctor....
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer is 2. Form the JSP 1.1 specs, (2.13.1 jsp:useBean)
If the object is not found in the specified scope; and the class specified names a nonabstract class that defines a public no-args constructor, then that class is instantiated, and
the new object reference is associated the with the scripting variable and with the specified name in the specified scope using the appropriate scope dependent association mechanism (see PageContext). After this, step 7 is performed.
If the object is not found, and the class is either abstract, an interface, or no public
no-args constructor is defined therein, then a
java.lang.InstantiationException shall occur. This completes the processing of this useBean action.
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you know. If you don't write any constructor for a Java class (and a bean is a Java class with some special extra-rules) an empty no-argument constructor is inserted in the code automatically at compile time.
If the no-arg-constructor is added by the programmer or by the compiler is not scope of the question.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do they say "must"..if no ctor is defined it means there is a noarg ctor....

You must explicitly define a public no-args constructor. Refer to Marty Hall's Core Servlets and JSPs for more info.
-anthony
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try out this link too
-anthony
 
Sathya Srinivasan
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make Anthony's point clear, here is the excerpt from CoreServlets book.

A bean class must have a zero-argument (empty) con-structor.
You can satisfy this requirement either by explicitly defining such a constructor or by omitting all constructors, which results in an empty constructor being created automatically. The
empty constructor will be called when JSP elements create beans.

All the specification says is, whether you explicitly define a no-arg constructor or if you don't define any constructors, thereby enabling the compile to create a default Java constructor, ultimately the bean MUST have a no-args constructor.
The MUST is given there to emphasize the point that the user should not forget to include a no-args constructor if he/she has defined other constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic