File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets 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: 60749
    
  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: 60749
    
  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: 60749
    
  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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call JavaBean Method from JSP
 
Similar Threads
use c:forEach tag inside another c:forEach tag
JSTL's EL. Problem iterating over arraylist
Pass value to ManagedBean in Facelet
jsp:useBean and default scope (head first mock exam question)
struts tags