• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

<jsp:useBean> compilation error vs runtime error

 
Sarvarth Bhatnagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Issue: compilation error vs runtime error

<jsp:useBean id="intBean" class="java.lang.Integer" />
<jsp:setProperty name="intBean" property="*"/>
<%=intBean%>

When we use the class as java.lang.Integer it would throws an Instantiation Exception

However, if we use <jsp:useBean id="intBean" class="com.myclass" /> such that

public class myclass{

myclass(int a){} //parametrized constructor
private int intBean;
//getter + setter of intBean

}
Then the <jsp:useBean would cause a compilation error.

Could anyone suggest the possible reason behind this?
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sarvath,

Your myclass needs a no-argument constructor.
<jsp:useBean> initializes the bean with the no-argument constructor of the specified class.

Hope this helps.
 
Sarvarth Bhatnagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalit Mehra wrote:Hi Sarvath,

Your myclass needs a no-argument constructor.
<jsp:useBean> initializes the bean with the no-argument constructor of the specified class.

Hope this helps.



Hi Lalit,
my question is why do we get compilation error when we use an argument constructor:: com.myclass(in this case).
and On the contrary, we get an Exception when we go for a predefined API WrappeClass, why dont we get compilation error here too?
java.lang.Integer also has an argument constructor


 
German Gonzalez-Morris
Ranch Hand
Posts: 264
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Assuming you have created the custom class correctly, it should compile in both cases.

Are you creating package called "com" ?

i.e.:





Otherwise, please post the compilation error you are facing.

--German

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic