• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:beans - class must be instantiated - but why?

 
Jim So
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:

Which of the given statements are correct regarding the following JSP page code?
<jsp:useBean id="myint" class="java.lang.Integer" />
<%=myint%>


Select 1 correct option.
a It will print 0.


b It will print an undefined number.


c It will not compile.


d It will throw exception at runtime.




Answer is c.
While translating the jsp file, the container determines that the given class cannot be instantiated without an argument and thus it fails to compile.


So where will the bean object get instantiated normally?
Because ive seen code before where the bean object is set in the request when you call the RequestDispatcher.
Then when you use <jsp:useBean>, it will work when you try and call attributes off it.

Im a little confused why the answer is c...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem here is that the java.lang.Integer class has no default constructor. So the container cannot instanciate the bean.
 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi christopher,

do you mean to say that none of the wrapper class has a default constructor. it calls the super constructor after we've called one of its constructor ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the container has to instantiate a class identified by the "class" attribute of the useBean tag, a default constructor must exist. If you look at a wrapper class like java.lang.Integer, there is no default constructor, so the container cannot instantiate it.
 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it now. Thanks Christopher.
 
Srinivas Rachamallla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Christopher,

First.jsp
---------
<jsp:useBean id="cust" class="org.com.Customer" />
org.com.Customer has default constructor and it has been instantiated.


Second.jsp
---------
<jsp:useBean id="cust1" class="org.com.Customer" />

now will the container create one more instance or allocates same instance.

thanks in advance.

Srinivas.
Eat The FROG.
"If you have to eat two frogs, eat the ugliest..."


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivas. It would have been better to start a new thread

now will the container create one more instance or allocates same instance.

In this case, the scope attribute is not set for the useBean tag, which means that the bean instance will be created at page scope. So a new bean will be created on a new request.
 
Swathi Kota
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer to Jim's question would be D. Because if the class cannot be instantiated, class gets complied properly and at runtime when class is loaded and servlet engines tries to instantiate class then java,lang.InstantiationException is thrown so answer is runtime error.

Please correct me if I am wrong.
 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:useBean id="myint" class="java.lang.Integer" />
<%=myint%>

This is what i got when i deployed the above code in tomcat.
"The value for the useBean class attribute java.lang.Integer is invalid."

I think Swati is right. There'll not be any compilation error but runtime error.
 
belakumari das
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:useBean id="myint" class="java.lang.Integer" />

is same as

Integer myint = new Integer()
But there is no Integer() constructor so it fails.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i got when i deployed the above code in tomcat.
"The value for the useBean class attribute java.lang.Integer is invalid."


this should be the message shown on jsp translation state before it reach compilation state, so it is surely not runtime error. It can be concluded as translation error or compilation error.
 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lee,

I don't think it should be a translation error. Translation error checks for the syntactic errors. it can be either a compilation or runtime error.

Can anyone explain me please the difference of compilation and runtime error wrt JSP ?

 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Sorry for the last line of my above post.
Can anyone explain me please the difference of compilation and runtime error wrt JSP ?
Don't know what made me write that line.

Anyways coming to the original question posed, this is surely a compilation error because it fails to instantiate the Integer class in the absence of a no-arg constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic