This question troubles me long time ago and i did not bother to find it out until recently. When creating a bean for jsp, my bean will work perfectly well if i don't include a constructor explicitly. If i provide a constructor for the bean, errors occur at <jsp:useBean .....>. Can somebody give me some explanation? Thanks a lot.
i'm surprised, a bean to work definitely needs a zero argument constructor (which ofcourse if provided if not specified). If you have a single / multiple arg constructor and if u have forgotten to define the zero argument constructor then the java class is not a bean. So it w'd'nt work. karthik.
Originally posted by SoonAnn Lim: This question troubles me long time ago and i did not bother to find it out until recently. When creating a bean for jsp, my bean will work perfectly well if i don't include a constructor explicitly. If i provide a constructor for the bean, errors occur at <jsp:useBean .....>. Can somebody give me some explanation? Thanks a lot.
Joined: Jun 21, 2001
In my case, if i do not supply a zero argument constructor, the bean works fine. Once i give any explicit constructor, errors occur. It seems like my bean allow only implicit constructor. That is, when a constructor is not specified in the code, a zero argument constructor is generated automatically by the compiler. Correct me if i am wrong.
Well, thats the rule. If you don't supply any constructor AND the super-class of your class has a no-args constructor, Java will supply a default no-args constructor. IF on the other hand, you supply a with-args constructor, Java assumes you know what you are doing and does not provide a default no-args constructor. Bill
Hi William, That is the odd thing i want to make it clear. I understand that default constructor will be supplied by Java supper class. However, i am confused when i supply a constructor (either with or w/o arguments) to my bean class, errors are generated. If i take off my constructor and used the default constructor from super class, everything behaves as it should. The key note is <b>Why errors are generated when i supply my constructor for my bean class?</b> Thanks for any comment.
That's kind of odd, that you supplied a no-arg constructor, and you called super() from within your constructor, as expected by the bean class, and you still get an error. The only thing I can think of is that, the bean class just does not expect any constructor (with/without args) to be supplied at all. So supplying one does not make it a bean.
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Author and all-around good cowpoke
Joined: Mar 22, 2000
Lets see the exact text of the error, and include the exact text of your jsp:useBean tag. Also the exact text of the constructor that causes the problem. (Incidently, if your Bean code is not in a package you can get weird results from JSP.) Bill
Joined: Jun 21, 2001
I wish i have my compute with me right now. I am not able to test the code until next week. However, if one of you is able to do me a favor, can you add a constructor to NumGuessBean.java which comes with Tomcat distriubtion in "examples" application. In the no argument constructor, can you make up one property and give a default value? I just think of this approach when i reply this post. One note, when i look at Marty's book, most of the bean used in JSP do not contain an explicit constructor, just bunch of setXXX and getXXX methods. The same style can be observed in many javaworld's example. Am I code it wrong in my class?