Page 1 of 1

About Question enthuware.jwpv6.2.791 :

Posted: Mon Aug 15, 2011 5:49 am
by ETS User
Don't you think that the answer to this question is the last option?

The translated code from the given JSP:

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);
}

The answer should be "It will throw an Exception"

Regards

Re: About Question enthuware.jwpv6.2.791 :

Posted: Wed Aug 17, 2011 4:14 pm
by admin
Yes, you are right. This should be fixed as per the given explanation.

thanks for your feedback!

Re: About Question enthuware.jwpv6.2.791 :

Posted: Sun Mar 03, 2013 1:29 pm
by BullDog
Hm, answer seems to be tricky.
I'm using Tomcat 7.0.22 and when I try to access very simple test page "test.jsp" with only following 2 lines:
<jsp:useBean id="myint" class="java.lang.Integer" />
<%=myint%>
I get
org.apache.jasper.JasperException: /test.jsp (line: 1, column: 1) The value for the useBean class attribute java.lang.Integer is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:408)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:149)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1234)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1182)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Generator.generate(Generator.java:3490)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:644)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

So, on one hand it is correct, that this is an exception. But on the other, this exception happens during compilation, so definitely if does not compile ;)

Anyway, point is taken - Bean should have no argument constructor. And keep fingers crossed that exactly this question will not be on the real exam :)

Re: About Question enthuware.jwpv6.2.791 :

Posted: Fri Sep 13, 2013 3:26 pm
by Jkike79
In Tomcat 6.0.29 I got the same behaviour described by BullDog.

Code: Select all

SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /newjsp.jsp(18,8) The value for the useBean class attribute java.lang.Integer is invalid.
Can anyone explain me why answer C is not correct?

I don't understand very well. I get an exception, this is clear, but the jsp cannot be translated into the corresponding java servlet and so it cannot be compiled.

Or, the word "exception" has to be meant as "translation exception" and not "runtime exception"?

Thank you.

Re: About Question enthuware.jwpv6.2.791 :

Posted: Fri Sep 13, 2013 4:17 pm
by admin
JSP was translated correctly into a servlet but at runtime, it was unable to create the bean. So this is an exception at execution time and not an error during translation time.

HTH,
Paul.

Re: About Question enthuware.jwpv6.2.791 :

Posted: Mon Sep 16, 2013 2:29 pm
by Jkike79
Thank you for the explanation.

Re: About Question enthuware.jwpv6.2.791 :

Posted: Mon Mar 14, 2016 2:50 am
by Igor Makarov
Hi there.

try {
myint = (java.lang.Integer) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "java.lang.Integer");
} catch (ClassNotFoundException exc)

I thought that Beans.instantiate is used only if we write <jps:useBean id='..' beanName='...'>
If we write <jps:useBean id='..' class='...'> then normal 'new' construction is being used (myInt = new Integer())

Therefore the right answer is 'It will not compile' because there is no empty Integer constructor

JSP 2.0 spec chapter 5.1
"beanName The name of a bean, as expected by the instantiate method
of the java.beans.Beans class.
This attribute can accept a request-time attribute expression
as a value."

Re: About Question enthuware.jwpv6.2.791 :

Posted: Tue Mar 15, 2016 9:04 pm
by admin
Section 5.1 says in Semantics section, point 6, "If the object is not found in the specified scope , and the class specified names a non-abstract class that defines a public no-args constructor, then the class is instantiated."

It doesn't say how the class is instantiated. Apparently, tomcat uses Beans.instantiate even in this case.

-Paul.

Re: About Question enthuware.jwpv6.2.791 :

Posted: Mon Mar 21, 2016 2:43 am
by Igor Makarov
admin wrote: ... Apparently, tomcat uses Beans.instantiate even in this case.

-Paul.
Hi Paul.

Agree that actual implementation is provider specific and we can't know the exact output.

In Test 6 question 21 QID: enthuware.jwpv6.2.972 I see that explanation states that Tomcat 7 uses 'new' operator
to create a bean for this useBean case

Code: Select all

<jsp:useBean id="mb" class="java.util.Hashtable"/> 

Code: Select all

mb = (java.util.Hashtable) _jspx_page_context.getAttribute("mb", javax.servlet.jsp.PageContext.PAGE_SCOPE);
if (mb == null) {
    mb = new java.util.Hashtable();
    jspx_page_context.setAttribute("mb", mb, javax.servlet.jsp.PageContext.PAGE_SCOPE);
}

Re: About Question enthuware.jwpv6.2.791 :

Posted: Mon Mar 21, 2016 9:48 pm
by admin
You are right. I am not really sure what can be done here. I think it would be best to leave this discussion here so that users can see the issue involved and take a call.
-Paul.

Re: About Question enthuware.jwpv6.2.791 :

Posted: Sun May 27, 2018 7:47 pm
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?

Re: About Question enthuware.jwpv6.2.791 :

Posted: Mon May 28, 2018 11:43 pm
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.