About Question enthuware.jwpv6.2.791 :
Moderator: admin
About Question enthuware.jwpv6.2.791 :
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
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
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
Yes, you are right. This should be fixed as per the given explanation.
thanks for your feedback!
thanks for your feedback!
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.jwpv6.2.791 :
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
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
-
- Posts: 11
- Joined: Tue Sep 10, 2013 3:20 pm
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
In Tomcat 6.0.29 I got the same behaviour described by BullDog.
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.
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.
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.
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 11
- Joined: Tue Sep 10, 2013 3:20 pm
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
Thank you for the explanation.
-
- Posts: 14
- Joined: Tue Sep 08, 2015 3:27 am
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
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."
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."
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
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.
It doesn't say how the class is instantiated. Apparently, tomcat uses Beans.instantiate even in this case.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Tue Sep 08, 2015 3:27 am
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
Hi Paul.admin wrote: ... Apparently, tomcat uses Beans.instantiate even in this case.
-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);
}
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
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.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Sat Apr 14, 2018 12:16 pm
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
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?
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?
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.791 :
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 3 guests