Developing a Simple Action

As you have seen in the previous chapters, a custom action element in a JSP page consists of a start tag (possibly with attributes), a body, and an end tag:

<prefix:actionName attr1="value1" attr2="value2">
  The body
</prefix:actionName>

If the action element doesn’t have a body, the following shorthand notation can be used instead of the start tag and the end tag:

<prefix:actionName attr1="value1" attr2="value2" />

A tag handler is the object invoked by the JSP container when a custom action is found in a JSP page. In order for the tag handler to do anything interesting, it needs access to all information about the request and the page, as well as the action element’s attribute values (if any). At a minimum, the tag handler must implement the Tag interface, which contains methods for giving it access to the request and page information, as well as methods called when the start tag and end tag are encountered. Note that an action element supported by a tag handler that implements the Tag interface may have a body, but the tag handler has more limited control over the body content than a tag handler that implements the BodyTag interface. For the attribute values, the JSP container treats the tag handler as a bean and calls a property setter method corresponding to each attribute, as shown in Figure 16.2.

Tag interface methods and property setter methods

Figure 16-2. Tag interface methods and property setter ...

Get Java Server Pages now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.