About Question enthuware.jwpv6.2.791 :

Moderator: admin

Post Reply
rishi2616
Posts: 3
Joined: Sat Apr 14, 2018 12:16 pm
Contact:

Re: About Question enthuware.jwpv6.2.791 :

Post by rishi2616 »

In another similar question, the explanation said something like this

Whenever there is only the class attribute for use bean tag, you should visualize it as:

classValue obj = new classValue();

Applying this logic here, we get:

Book obj = new Book();

It is clear that for this to compile, Book cannot be an abstract class or an interface otherwise new Book() will fail. Besides this, Book class must also have a public no-args constructor.

But here it says

The only requirement for a class to be used as a bean in a JSP page is that the class should have a public no-args constructor so that the JSP engine can instantiate the class. The following is the relevant portion of the code generated by tomcat for this page:

    try {
        myint = (java.lang.Integer) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "java.lang.Integer");
    } catch (ClassNotFoundException exc) {
        throw new InstantiationException(exc.getMessage());
    } catch (Exception exc) {
        throw new ServletException (" Cannot create bean of class "+"java.lang.Integer", exc);
    }

So, the Beans.instantiate() method throws the following exception:

java.lang.InstantiationException: class java.lang.Integer : java.lang.InstantiationException: java.lang.Integer
    at org.apache.jsp.a$jsp._jspService(a$jsp.java:82)
    ...


My question is what actually happens during translation time and request time when a bean is defined using jsp:useBean action. For these kinds of questions where the bean can't be instantiated due to the absence of a no-arg constructor, how should we actually visualize it?

admin
Site Admin
Posts: 10389
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.791 :

Post by admin »

The detailed code given above is what actually happens. For the purpose of the exam, however, the short snippet is a short cut that will help you arrive at the answer quickly.

Post Reply

Who is online

Users browsing this forum: No registered users and 167 guests