Page 1 of 1

About Question com.enthuware.ets.scwcd.v5.2.388 :

Posted: Wed Apr 13, 2011 7:05 pm
by ETS User

Code: Select all

<jsp:useBean id="myint" class="java.lang.Integer" />
I am confused. The answer says it cannot compile but in the explanation it says Instantiation exception. Could somebody provide more insight on this one please.

Re: About Question com.enthuware.ets.scwcd.v5.2.388 :

Posted: Thu Apr 14, 2011 1:29 pm
by admin
It will compile but will throw an instantiation exception at runtime because when the container tries to create an instance of Integer class, it will fail as there is no default constructor for Integer class.

Re: About Question com.enthuware.ets.scwcd.v5.2.388 :

Posted: Mon Jun 10, 2013 9:52 pm
by marcelomnc
So the correct answer will be "It will throw exception at runtime." right ??? But the highlighted answer by the system is "It will not compile." I think that this is some kind of ERRATA.

Am I right ?

Re: About Question com.enthuware.ets.scwcd.v5.2.388 :

Posted: Tue Jun 11, 2013 5:53 pm
by marcelomnc
Actualy i have recreated and tested the code and these are my foundings.

testUseBean.jsp

Code: Select all

<jsp:useBean id="myint" class="java.lang.Integer"/>
<%=myint%>
Testing testUseBean.jsp in tomcat 6.0.32 with the url http://localhost:8080/test/testUseBean.jsp i got this output:
Jun 11, 2013 5:43:59 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /testUseBean.jsp(1,1) The value for the useBean class attribute java.lang.Integer is invalid.
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
at org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1231)
at org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1178)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
...
According to this, it really seems like "it will not compile" is the correct answer, perhaps the compiler is clever enough to find out that java.lang.Integer cannot be instantiated without arguments.

Re: About Question com.enthuware.ets.scwcd.v5.2.388 :

Posted: Fri Jun 14, 2013 6:58 am
by admin
It really depends on what the JSP code is translated into. I think what you've observed is correct and that shows that it is translated into myint = new Integer(); which will fail to compile.
However, as the explanation notes, it is translated into:
myint = (java.lang.Integer) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "java.lang.Integer");

In this case, the compilation should succeed.

So I think the answer to this question is not certain :( Should be updated.
-Paul.