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:

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

A Java class referred to as a tag handler implements the action’s behavior. In order for the tag handler to do anything interesting, it needs access to the request and scope information, as well as the action element’s attribute values (if any). The container calls methods defined in the Tag interfaces to provide it with this information. For the attribute values, the JSP container treats the tag handler as a bean and calls a property setter method corresponding to each attribute. The Tag interface also contains methods called by the container when the start tag and end tag are encountered, as shown in Figure 20-2.

Tag interface methods and property setter methods

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

A tag handler that implements just the Tag interface can add dynamic content to the response body and set response headers, add or remove variables in one of the JSP scopes, and tell the container to either include the action element’s body in the response or ignore it.

Here are the most important methods ...

Get JavaServer Pages, Second Edition 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.