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

The new JavaServer Pages (JSP) version incorporates the expression language (EL) introduced by the JSP Standard Tag Library (JSTL) to let Web designers produce scriptless JSP pages that don't contain Java code. Since JSP 2.0 provides backward compatibility to JSP 1.x, you may still include Java snippets in your pages, but tag handlers and JavaBean components are much better places for Java-based functionality.

JSP 2.0 provides new features for tag handlers such as dynamic attributes, the Simple Invocation Protocol, and .tag files. You still use the old JSP 1.0 standard actions for creating JavaBean instances and setting their properties, but now you can access bean properties, request parameters, and JSP attributes/variables with the new expression language.

All those JSP technology improvements let you achieve the goal of separating the JSP/HTML markup from the Java code. One thing is missing, however. JSP 2.0 has no syntax for calling a public nonstatic JavaBean method from a scriptless JSP page. This article solves that issue by providing a JSP 2.0 simple tag with dynamic attributes.

Note: You can download this article's source code from Resources.

Expression language needed

Suppose you have a java.util.List instance you must present as an HTML list. Here is a quick solution based on JSP 1.x:

<UL>
<%
    Iterator iterator = list.iterator();
    while (iterator.hasNext()) {
        Object elem = iterator.next();
        if (elem == null)
            elem = "";
%>
        <LI> <%= elem %> </LI>
<%
    }
%>
</UL>


Existing JSP-based Web applications consist of Java code mixed with HTML markup like the above code fragment. Maintaining hundreds of pages like that can be a nightmare if you have separate Java development and Web design teams. The solution is to move the Java code into tag libraries so that developers can do their jobs without pasting Java code within Webpages and designers can edit their Webpages without worrying about breaking the Java code.

However, JSP 1.x has several problems that don't let you easily develop scriptless JSP pages. Until recently, no standard method existed for accessing Java objects from a JSP page without using Java code. In addition, coding tag handler classes wasn't as simple as it could have been.

The following lines of code are based on JSTL 1.0, which can be used with JSP 1.2. The <c:forEach> tag iterates over the elements of the given list and exports the elem variable for each element. Instead of declaring elem as a local variable, the <c:forEach> tag creates a page attribute with pageContext.setAttribute(). This attribute's value is printed with JSTL's <c:out> tag:

<UL>
<c:forEach var="elem" items="${list}">
    <LI> <c:out value="${elem}"/> </LI>
</c:forEach> 
</UL>


JSTL provides standard tags for processing XML documents and accessing relational databases along with formatting tags, internationalization tags, conditional tags, iterator tags, URL-related tags, and other general-purpose tags. JSTL has solved many of JSP 1.x's problems with the help of an expression language that allows you to access Java objects from JSP pages without using Java code. For example, instead of looking for an attribute or accessing a request parameter with:

  • Print
  • Feedback

Resources