*
The moose likes JSP and the fly likes Call JavaBean Method from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Call JavaBean Method from JSP" Watch "Call JavaBean Method from JSP" New topic
Author

Call JavaBean Method from JSP

John Storta Jr.
Greenhorn

Joined: Jul 26, 2009
Posts: 29
This is in some ways a hypothetical question, though I have run into some situations where I have needed to do this and I cannot find any documentation indicating it is possible.

I have a fairly basic JavaBean. I have no issues initializing it and getting/setting properties. What I cannot find any information on is how to call a plain old method in the bean from within a JSP page. By 'plain old method' I mean one that is not a getter or a setter.

Here is some sample code.




What I would like to do is something like this.



Where doSomethingCool() is a method defined in the bean.

I usually get around this by just creating a method called something like setDoSomethingCool(String ignoreMe), and then using setProperty to call the method.

Is it not possible to call a plain old method from within a JSP page?
If not, is there a reason?


Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

John Storta Jr. wrote:Is it not possible to call a plain old method from within a JSP page?

Nope. Doesn't follow the JavaBean specification.

If not, is there a reason?

See above.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

P.S. You can call static methods by setting up an EL function, but not instance methods.
John Storta Jr.
Greenhorn

Joined: Jul 26, 2009
Posts: 29
Thanks Bear.

I think I have not been using beans properly then. I will have to look into whether or not what I am actually trying to do can be accomplished with static methods or perhaps change my code within the accessor methods.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

Or, take a step back and check that what you are doing is appropriate for a JSP. Many times, if you find yourself having trouble doing something, it's because it should have already been handled by the controller.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Call JavaBean Method from JSP