my dog learned polymorphism*
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 Android Security Essentials Live Lessons this week in the Android 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: 60817
    
  65

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: 60817
    
  65

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: 60817
    
  65

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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Call JavaBean Method from JSP
 
Similar Threads
use c:forEach tag inside another c:forEach tag
struts tags
Pass value to ManagedBean in Facelet
JSTL's EL. Problem iterating over arraylist
jsp:useBean and default scope (head first mock exam question)