Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialize javabean constructor with parameter

 
Jennifer Johnson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help! I am trying to initialize a bean by passing a parameter to the constructor. I need the scope to be "application". I know I have to have a zero arg constructor, but how do I have one that accepts paramters, and can be called from a JSP page?
Thanks so much!!!
------------------
Jen
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a useBean JSP tag, you can't invoke any constructor but the no-arg constructor. That leaves two avenues open: either you perform the initialisation in a setter method:Note that setFoo() is only invoked once, when the bean is instantiated - in general, the body of a useBean tag is evaluated only when the bean is instantiated.
Or you instantiate the bean outside a useBean tag:Subsequent pages can use useBean in the usual way to access the bean.
- Peter
 
Jennifer Johnson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter...I appreciate the alternatives! I've been trying to get the alternate constructor to work for days!
------------------
Jen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic