Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Call JavaBean methods from JSP 2.0 pages

Develop custom tags with dynamic attributes

  • Print
  • Feedback

Page 2 of 6

<%= pageContext.findAttribute("a") %>
<%= request.getParameter("p") %>


you may now use:

${a}
${param.p}


You can access the JSP page context objects, page/request/session/ application attributes (also known as JSP variables), JavaBean properties, collection elements, request parameters, initialization parameters, cookies, and HTTP headers.

With JSP 1.2, the expression language is available only to JSTL-based applications and tag libraries. JSP 2.0 makes the EL available to all JSP applications and all tag libraries (including the old taglibs designed for JSP 1.x). JSP 2.0 also simplifies tag library development, as you'll see later in this article.

Since its first version, JSP has provided standard tags for using JavaBeans in JSP pages. You can create or find JavaBean instances with <jsp:useBean>, and then you can get and set their properties with <jsp:getProperty> and <jsp:setProperty>. With JSP 2.0, you may also get the value of a property with:

${bean.property}


In addition to properties, JavaBean components have public methods that often must be called from JSP pages. The remainder of this article will present three ways for calling JavaBean methods without using Java code. One is based on the JSP 2.0 support for functions, which are EL constructs that allow you to call Java classes' static methods. Another solution uses custom tags that get the method parameters as tag attributes. The third way is based on a generic tag that lets you call any public method of any JavaBean class from a JSP page.

Use functions

The initial JSTL 1.0 EL lacked support for functions. The JSP 2.0 EL lets you call a Java class's public static method using the following syntax:

${prefix:methodName(param1, param2, ...)}


The JSP function must be declared in a tag library descriptor (TLD):

<function>
    <name>methodName</name>
    <function-class>className</function-class>
    <function-signature>
        returnType methodName(param1Type, param2Type, ...)
    </function-signature>
</function>


The Java class doesn't have to implement any special interface. The only requirement is to make the Java method public and static.

The TestBean class

The TestBean class has a public method named testMethod(), which is called from the JSP pages presented in the following sections. The JavaBean has three properties named text, number, and logic. These properties are modified by testMethod(), which returns a string containing the three properties' modified values:

package com.devsphere.articles.calltag;
public class TestBean {
    private String text;
    private int number;
    private boolean logic;
    public TestBean() {
        text = "";
        number = 0;
        logic = false;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public boolean getLogic() {
        return logic;
    }
    public void setLogic(boolean logic) {
        this.logic = logic;
    }
    public String testMethod(String text, int number, boolean logic) {
        setText(getText() + text);
        setNumber(getNumber() + number);
        setLogic(getLogic() || logic);
        StringBuffer buf = new StringBuffer();
        buf.append(getText());
        buf.append(' ');
        buf.append(getNumber());
        buf.append(' ');
        buf.append(getLogic());
        return buf.toString();
    }
}


The TestFunction class

Because the JSP 2.0 EL allows only calls to static methods, TestBean's testMethod() must be wrapped in a static method. The TestFunction class provides such a static wrapper that takes the same parameters as the bean method plus the bean object whose method must be called:

  • Print
  • Feedback

Resources