4

How can I call a bean method in JSP without using scriplets such as <% myBean.method() %>?

I have created my application in JSP. I know it is possible using JSF, but I don't want to create a new project.

Is it possible to use JSP or JSTL to solve this problem and call a bean method?

5
  • There are many ways for this. You forgot to elborate the concrete functional requirement for this. So it's not possible to propose the right way for this. Please edit your question accordingly if you'd like to know the right way. Commented Dec 26, 2012 at 20:58
  • @BalusC I want to call a function other than getter and setter Commented Dec 27, 2012 at 14:37
  • You have still not elaborated the concrete functional requirement for this. What exactly is the problem you're trying to solve by "call a function other than getter and setter". Commented Dec 27, 2012 at 14:46
  • @BalusC i saw yout tutorial as well. I want to call a fucnction in my bean that actually inserts the data in the database.. i want to call it from jsp page Commented Jan 1, 2013 at 16:35
  • I'd just like to point out that calling a database function from your view logic (e.g. JSP) is probably a bad idea... Commented Sep 21, 2015 at 13:20

3 Answers 3

3

You can call a bean method using EL. Just pass a reference of the class that has the method to JSP and call it like this: ${objectName.methodName()}

Sign up to request clarification or add additional context in comments.

1 Comment

There's one major caveat starters easily overlook: It gets invoked on page load, not on form submit.
0

This is not possible in JSP,a better solution is to use JSF.Read about the actionListener in JSF.IN order to achieve this function in JSP you have to create custom Tag.

Comments

0

callBean.jsp

<HTML>
<BODY>
<jsp:useBean id="bean" class="form.Bean" />
Message is: <jsp:getProperty name="bean" property="text" /> 
</BODY>
</HTML>

Bean.java

package form;
public class Bean
{
private String text;
public String getText()
{
return text;
}
public Bean()
{
 text="Hello World";
}
}

5 Comments

This is for the property.what if my bean class contain a fucntion that is connectToDb() and i want to call it
in that case, try this ${Bean.getText()}
And, as per my knowledge, bean have only setter and getter. So, ultimately, u would be returning property from your bean. isn't ?? I really don't think, there would be anything wrong in my solution. Perhaps, it satisfy you need.
that doesn't run,If we try like ${bean.functionName}
see my previous comment, i told you. Since, bean have only setter (to set the property) and getter (to get the value). My solution is exactly works as getter (to get the property value). Other than this, i don't think, it would be possible to call normal function. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.