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

Moderator: admin

Post Reply
ETS User

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

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

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.

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

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

Post by admin » Thu Apr 14, 2011 1:29 pm

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.
If you like our products and services, please help us by posting your review here.

marcelomnc
Posts: 5
Joined: Mon Jun 10, 2013 9:44 pm
Contact:

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

Post by marcelomnc » Mon Jun 10, 2013 9:52 pm

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 ?

marcelomnc
Posts: 5
Joined: Mon Jun 10, 2013 9:44 pm
Contact:

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

Post by marcelomnc » Tue Jun 11, 2013 5:53 pm

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.

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

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

Post by admin » Fri Jun 14, 2013 6:58 am

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests